Searching for Frame in current header and delete it

A

andreas

Dear Experts:

For some specific reason I inserted a frame into the current even page
header. How can I delete it via VBA on the condition that the cursor
resides in the page (of the main document) whose header has got this
frame?

Other headers, ie. odd page headers and first page header of the
current section must not be deleted.
Other headers in other sections may also have a frame in the header,
but they must not be deleted, either.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
 
P

Pesach Shelnitz

Hi Andreas,

The following macro will remove all frames from the header for even pages in
the current section. (The current section is the section in which the cursor
is located).

Sub RemoveFrameFromCurrentEvenHeaderFooter()
Dim i As Long
Dim myRange As Range

Set myRange = Selection.Sections(1).Headers(wdHeaderFooterEvenPages).Range
With myRange
For i = .Frames.Count To 1 Step -1
.Frames(i).Delete
Next
End With

Set myRange = Nothing
End Sub
 
A

andreas

Hi Andreas,

The following macro will remove all frames from the header for even pagesin
the current section. (The current section is the section in which the cursor
is located).

Sub RemoveFrameFromCurrentEvenHeaderFooter()
    Dim i As Long
    Dim myRange As Range

    Set myRange = Selection.Sections(1).Headers(wdHeaderFooterEvenPages).Range
    With myRange
        For i = .Frames.Count To 1 Step -1
           .Frames(i).Delete
        Next
    End With

    Set myRange = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz
My Web site:http://makeofficework.com










- Show quoted text -

Hi Pesach,

great, it is working just fine. Thank you very much for your terrific
help.

There is one thing I would like to ask you:

From the main document I am not able to see whether the current header
where the frame is located is a "First Page Header", "EvenPageHeader"
or a "PrimaryPageHeader".
I would have to access the header to find out.

Is it possible to rewrite the code so that REGARDLESS of the HEADER
TYPE, the frame of the header of the current page is deleted?

I hope I could make myself clear and it is not too demanding.

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
P

Pesach Shelnitz

Hi Andreas,

The macro in my previous post removes frames only from the even-page header
in the current section and does not even look at the other headers. This is
done by setting a Range object equal to the Range object of the even-page
header. Of course, this code could be modified to do the same for all the
headers in the current section, or even in all sections. It is also possible
to add code that will pop up a message telling the user that a frame was
found in header X and ask for confirmation before removing it. Would you like
macros that do any of these things?
 
A

andreas

Hi Andreas,

The macro in my previous post removes frames only from the even-page header
in the current section and does not even look at the other headers. This is
done by setting a Range object equal to the Range object of the even-page
header. Of course, this code could be modified to do the same for all the
headers in the current section, or even in all sections. It is also possible
to add code that will pop up a message telling the user that a frame was
found in header X and ask for confirmation before removing it. Would you like
macros that do any of these things?

--
Hope this helps,
Pesach Shelnitz
My Web site:http://makeofficework.com













- Show quoted text -

Dear Pesach,

thank you very much for your superb help. Actually there is one thing
that would suffice:

Macro code to tell me which is the current header (on the condition
that my cursor is located in the main document and I select "View -
Header-Footers" from the Main Menu):
For example: the code should tell me that "... the current header is
located on section x, adjusted page number x and is a primary or even
or first page header".

I hope this is not beyond the scope of this forum.

Thank you very much in advance.

Regards, Andreas
 
P

Pesach Shelnitz

Hi Andreas,

The following macro will display the information that you want if you place
the cursor in the header. I don't know how to get all of this information in
a macro when
the cursor is in the main text.

Sub HeaderTypePageSection()
Dim hdType As String

With Selection
Select Case .Information(wdHeaderFooterType)
Case 1
If .Sections(1).PageSetup. _
DifferentFirstPageHeaderFooter Then
hdType = "odd"
Else
hdType = "primary"
End If
Case 3
hdType = "even"
Case 4
hdType = "first"
End Select
MsgBox "The current header is the " & hdType & _
" page header on Page " & _
.Information(wdActiveEndPageNumber) & _
" in Section " & _
.Information(wdActiveEndSectionNumber)
End With
End Sub
 
A

andreas

Hi Andreas,

The following macro will display the information that you want if you place
the cursor in the header. I don't know how to get all of this informationin
a macro when
the cursor is in the main text.

Sub HeaderTypePageSection()
    Dim hdType As String

    With Selection
        Select Case .Information(wdHeaderFooterType)
            Case 1
                If .Sections(1).PageSetup. _
                    DifferentFirstPageHeaderFooter Then
                    hdType = "odd"
                Else
                    hdType = "primary"
                End If
            Case 3
                hdType = "even"
            Case 4
                hdType = "first"
        End Select
        MsgBox "The current header is the " & hdType & _
        " page header on Page " & _
        .Information(wdActiveEndPageNumber) & _
        " in Section " & _
       .Information(wdActiveEndSectionNumber)
    End With
End Sub

--
Hope this helps,
Pesach Shelnitz
My Web site:http://makeofficework.com











- Show quoted text -

Hi Pesach,

thank you very much for your swift help. I just tried it out.
Everything is working fine with the exception of the "Even header"
case.
In that case the message box does not list "even" but nothing. For the
trial I chose the following Section Page Set up:

- Different First Page Header and Different Even/Odd Page Headers

Any idea why this is so.

Thank you again.

Regards, Andreas
 
P

Pesach Shelnitz

Hi Andreas,

I mixed up a bit. Here is a corrected version of the macro.

Sub HeaderTypePageSection()
Dim hdType As String

With Selection
Select Case .Information(wdHeaderFooterType)
Case 0
hdType = "even"
Case 1
If .Sections(1).PageSetup. _
OddAndEvenPagesHeaderFooter Then
hdType = "odd"
Else
hdType = "primary"
End If
Case 4
hdType = "first"
Case Else
MsgBox "The cursor is not in a header."
Exit Sub
End Select
MsgBox "The current header is the " & hdType & _
" page header on Page " & _
.Information(wdActiveEndPageNumber) & _
" in Section " & _
.Information(wdActiveEndSectionNumber)
End With
End Sub
 
A

andreas

Hi Andreas,

I mixed up a bit. Here is a corrected version of the macro.

Sub HeaderTypePageSection()
    Dim hdType As String

    With Selection
        Select Case .Information(wdHeaderFooterType)
            Case 0
                hdType = "even"
            Case 1
                If .Sections(1).PageSetup. _
                    OddAndEvenPagesHeaderFooter Then
                    hdType = "odd"
                Else
                    hdType = "primary"
                End If
            Case 4
                hdType = "first"
            Case Else
                MsgBox "The cursor is not in a header."
                Exit Sub
        End Select
        MsgBox "The current header is the " & hdType & _
        " page header on Page " & _
        .Information(wdActiveEndPageNumber) & _
        " in Section " & _
       .Information(wdActiveEndSectionNumber)
    End With
End Sub

--
Hope this helps,
Pesach Shelnitz
My Web site:http://makeofficework.com











- Show quoted text -

Hi Pesach,

that's it. Thank you very much for your professional help. I really
appreciate it. Regards, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top