HeaderFooter

G

Greg Maxey

Today I was playing around with a macro to add a new section and kill
the link to previous header and footer.

Some apparently working code is posted at the end of this message if
anyone is interested.

While working on the macro, I first thought that I would use the
HeaderFooter property. Word help says"

Returns a HeaderFooter object for the specified selection or range.
Read-only.
Note An error occurs if the selection isn't located within a header
or footer.

Key terms are "specified selection or RANGE" and "if the selection
ISN'T located within a header or footer"

I think I met that criteria with the code below and get the error:
Method or data member not found on line using the HeaderFooter
property.

Sub AddSectionAndKillLinkToPrevious3()
Dim i As Long
Dim j As Long
Dim oDoc As Word.Document
Dim myRng As Word.Range
Set oDoc = ActiveDocument
Selection.InsertBreak Type:=wdSectionBreakNextPage
'Get the index number of the added section
i = oDoc.Range(0, Selection.Sections(1).Range.End).Sections.Count
Set myRng = oDoc.Sections(i).Headers(wdHeaderFooterPrimary).Range
myRng.Select
myRng.HeaderFooter.LinkToPrevious = False
End With
End Sub

It seems that HeaderFooter will only be accepted as a propert of a
specified Selecton. Am I missing something?

Code that seems to work:

Sub AddSectionAndKillLinkToPrevious()
Dim i As Long
Dim j As Long
Dim oDoc As Word.Document
Dim myRng As Word.Range
Set oDoc = ActiveDocument
Selection.InsertBreak Type:=wdSectionBreakNextPage
'Get the index number of the added section
i = oDoc.Range(0, Selection.Sections(1).Range.End).Sections.Count
With oDoc.Sections(i)
For j = 1 To 3
.Headers(j).LinkToPrevious = False
.Footers(j).LinkToPrevious = False
Next j
End With
End Sub
 
D

Doug Robbins - Word MVP

Hi Greg,

Here is another way:

Dim i As Long
Dim j As Long
Dim hf As HeaderFooter
Dim oDoc As Document
Dim myRng As Range
Set oDoc = ActiveDocument
Selection.InsertBreak Type:=wdSectionBreakNextPage
'Get the index number of the added section
i = oDoc.Range(0, Selection.Sections(1).Range.End).Sections.Count
For Each hf In oDoc.Sections(i).Headers
hf.LinkToPrevious = False
Next hf
For Each hf In oDoc.Sections(i).Footers
hf.LinkToPrevious = False
Next hf


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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