What is happening? Headers and Footers Q

A

androo...

I tried to write a simple macro that when fired (with the selection point
between two next page section breaks) would delete the header and footer for
the section but not for any sections before or after. However the result
seems to be almost random deletions or error messages. Amazingly, the
results I get are not the same if I run the macro compared with stepping
through it. Here is the draft code I used:

Sub delete_HF4()
'Header
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.NextHeaderFooter
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.PreviousHeaderFooter
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1

'Footer
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.NextHeaderFooter
Selection.HeaderFooter.LinkToPrevious = False
ActiveWindow.ActivePane.View.PreviousHeaderFooter
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1

'Main Doc
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub


I then tried a different approach but got similar results. I found that
putting mgsBox's before and after some lines made the macro work (or at
least become predictable) so I wonder if things are not updating in time for
the next line of code??? Any help would be appreciated.

Thanks androooooooooo...............

p.s. I also tried this code
Selection.HeaderFooter.LinkToPrevious = Not
Selection.HeaderFooter.LinkToPrevious

p.p.s. below is my other clunky and unsuccessful approach

Sub deleteHF3()
Dim mySection As Long
mySection = Selection.Sections(1).Index 'Remember current section
'Header
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader 'Enter
header on this page
Selection.HeaderFooter.LinkToPrevious = False 'Do not link to previous
section
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'Exit header
to main document

Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst,
Count:=(mySection + 1), Name:="" 'Goto next section
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader 'Enter
header on this page
Selection.HeaderFooter.LinkToPrevious = False 'Do not link to previous
section
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'Exit header
to main document

Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst,
Count:=(mySection), Name:="" 'Goto original section
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader 'Enter
header on this page
Selection.WholeStory 'Select entire header in this section
Selection.Delete Unit:=wdCharacter, Count:=1 'Delete entire header in
this section
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'Exit header
to main document

'Footer
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter 'Enter
Footer on this page
Selection.HeaderFooter.LinkToPrevious = False 'Do not link to previous
section
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'Exit header
to main document

Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst,
Count:=(mySection + 1), Name:="" 'Goto next section
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter 'Enter
Footer on this page
Selection.HeaderFooter.LinkToPrevious = False 'Do not link to previous
section
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'Exit header
to main document

Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst,
Count:=(mySection), Name:="" 'Goto original section
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter 'Enter
header on this page
Selection.WholeStory 'Select entire header in this section
Selection.Delete Unit:=wdCharacter, Count:=1 'Delete entire header in
this section
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'Exit header
to main document
End Sub
 
S

Shauna Kelly

Hi Androo

The real problem with your code is the use of .NextHeaderFooter. Word's Help
says that, when you invoke this, "If the selection is in a header, this
method moves to the next header within the current section (for example,
from an odd header to an even header) or to the first header in the
following section."

So, depending on where your cursor is, you're at the mercy of what Word
thinks is the 'next' header or footer, which may or may not be in the
current section. That explains the unpredictable results.

The description of what you want is fairly clear (modified slightly):
- start with the selection point between two next page section breaks
- delete the header and footer for the selected section
- but not for any sections before or after. This means that:
- the current section must not be linked to its predecessor
- the next section must not be linked to the current section

So, I tried to replicate what you wanted in code. Obviously you have to
un-hitch the sections before you delete the content in the current section's
headers and footers. Here's one way to do it:

Sub DeleteHAndFAtTheSelection()

Dim secSelected As Word.Section
Dim secNext As Word.Section

'Get the section at the selection
Set secSelected = Selection.Sections(1)

'Unhitch the selected section's headers and footers
'from its predecessor
With secSelected.Headers
.Item(wdHeaderFooterEvenPages).LinkToPrevious = False
.Item(wdHeaderFooterFirstPage).LinkToPrevious = False
.Item(wdHeaderFooterPrimary).LinkToPrevious = False
End With
With secSelected.Footers
.Item(wdHeaderFooterEvenPages).LinkToPrevious = False
.Item(wdHeaderFooterEvenPages).LinkToPrevious = False
.Item(wdHeaderFooterEvenPages).LinkToPrevious = False
End With

'Get the next section, if there is one
On Error Resume Next
Set secNext = ActiveDocument.Sections(secSelected.Index + 1)
On Error GoTo 0

If Not secNext Is Nothing Then
'There is a 'next' section.
'So unhitch the next section's headers and footers
'from its predecessor
With secNext.Headers
.Item(wdHeaderFooterEvenPages).LinkToPrevious = False
.Item(wdHeaderFooterFirstPage).LinkToPrevious = False
.Item(wdHeaderFooterPrimary).LinkToPrevious = False
End With
With secNext.Footers
.Item(wdHeaderFooterEvenPages).LinkToPrevious = False
.Item(wdHeaderFooterEvenPages).LinkToPrevious = False
.Item(wdHeaderFooterEvenPages).LinkToPrevious = False
End With
End If

'Delete the content of the selected section's headers and footers
With secSelected.Headers
.Item(wdHeaderFooterEvenPages).Range.Delete
.Item(wdHeaderFooterFirstPage).Range.Delete
.Item(wdHeaderFooterPrimary).Range.Delete
End With
With secSelected.Footers
.Item(wdHeaderFooterEvenPages).Range.Delete
.Item(wdHeaderFooterEvenPages).Range.Delete
.Item(wdHeaderFooterEvenPages).Range.Delete
End With

End Sub


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
A

androo...

Wow. This even does even pages and first pages which I was going to tackle
after I got the first bit working :)

many thanks,
androooooooooooooooooo........
 

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