Deleting wrong header/footer

J

JohnTheTemp

I recorded a macro to create a blank page at the end of the doc then clear
out the header and footer of that last page.

However, when I run it, it clears out the header and footer of page 1.

Here's the part that clears the Header:

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.LinkToPrevious = False
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

When I stepped thru it, the focus was in the last page header until the
EndKey statement when it suddenly changed to Page 1 header. Que pasa?
 
G

Greg Maxey

John,

Recorded macros often come up short. Try:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
oRng.Collapse wdCollapseEnd
oRng.InsertBreak wdSectionBreakNextPage
With ActiveDocument.Sections.Last
With .Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range = ""
End With
With .Footers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range = ""
End With
End With
End Sub
 
D

Doug Robbins - Word MVP

Headers are properties of Sections, not pages. You would need to insert a
Section Break at the end of the document (thus creating a new Section at the
end) and then delete the header from that section.

--
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
 
J

JohnTheTemp

Sorry, I neglected to say that I created that blank page using a section
break. The macro was supposed to break the links to the previous header and
footer and clear them out.
 
D

Doug Robbins - Word MVP

With ActiveDocument.Sections(ActiveDocument.Sections.Count)
.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
.Footers(wdHeaderFooterFirstPage).Range.Delete
.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
.Footers(wdHeaderFooterPrimary).Range.Delete
.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
.Headers(wdHeaderFooterFirstPage).Range.Delete
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
.Headers(wdHeaderFooterPrimary).Range.Delete
End With


--
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