PDA

View Full Version : Solved: Deleting headers and footers throughout document



bobjohnson
12-10-2008, 01:36 PM
Hi, im trying to create a VBA macro for word 2003 that will delete all of the header and footer information throughout a document.

This is what ive come up with so far:



Sub PullHeadFoot()
ActiveWindow.View.Type = wdPrintView
ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
Selection.HomeKey Unit:=wdStory
cursec = 1
curpg = 1
Do
ActiveDocument.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Delete
ActiveDocument.ActiveWindow.View.SeekView = wdSeekCurrentPageFooter
Selection.WholeStory
Selection.Delete
Selection.MoveDown Unit:=wdScreen, Count:=1
Selection.MoveDown Unit:=wdWindow, Count:=-1
curpg = curpg + 1
Loop Until curpg = ActiveDocument.ActiveWindow.Panes(1).Pages.Count + 1
Do
ActiveDocument.Sections(cursec).headers(wdHeaderFooterPrimary).Range.Delete
ActiveDocument.Sections(cursec).Footers(wdHeaderFooterPrimary).Range.Delete
ActiveDocument.Sections(cursec).headers(wdHeaderFooterEvenPages).Range.Dele te
ActiveDocument.Sections(cursec).Footers(wdHeaderFooterEvenPages).Range.Dele te
ActiveDocument.Sections(cursec).headers(wdHeaderFooterFirstPage).Range.Dele te
ActiveDocument.Sections(cursec).Footers(wdHeaderFooterFirstPage).Range.Dele te
cursec = cursec + 1
Loop Until cursec = ActiveDocument.Sections.Last.Index + 1
End Sub




However, this macro sometimes deletes all of the headers and footers, but also sometimes leaves the headers and footers existing in the document but with 1 blank line. Ive also seen it miss a header/footer, but that was a one-off occassion.

Can anyone help me? Im sure theres got to be a simpler and more consistent way but i have no idea how to do it.

fumei
12-11-2008, 12:24 PM
Use objects.

Sub PullHeadFoot()
Dim oHF As HeaderFooter
Dim oSection As Section
For Each oSection in ActiveDocument.Sections
For Each oFF In oSection.Headers
oFF.Range.Delete
Next
For Each oFF In oSection.Footers
oFF.Range.Delete
Next
Next
End Sub


Note: "but also sometimes leaves the headers and footers existing in the document"

Headers and Footers always exist in documents. Always. Whether you have put anything in them, or taken anything out, they always exist. A brand new blank document has headers and footers.

bobjohnson
12-12-2008, 12:55 PM
That worked brilliantly. It doesnt get stuck inside the header and footer sections when the macro is finished now.

Also i wasnt aware that headers and footers always existed. I was under the impression they could be removed completely.

Thanks for your help!

lucas
12-12-2008, 01:07 PM
Hey Bob,
You really should use Option Explicit at the top of each module, form, etc.

If you had you would have received an error stating that a variable (oFF) was not defined.

Gerry made a little mistake in his post by diming oHF as HeaderFooter and then using oFF in the code.

I would suggest that this is what Gerry was intending to offer:


Option Explicit

Sub DeleteHeadFoot()
Dim oHF As HeaderFooter
Dim oSection As Section
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Headers
oHF.Range.Delete
Next
For Each oHF In oSection.Footers
oHF.Range.Delete
Next
Next
End Sub

fumei
12-12-2008, 01:15 PM
Oooooops! That is what you get for typing code in directly.

Steve is quite correct. Both in noting my error, and suggesting you use Option Explicit.

bobjohnson
12-12-2008, 01:19 PM
Yeah i caught that and fixed it :) I knew what he was trying to do. It worked really well and is less lines of code than my version, and it runs faster as well.

I do use option explicit though in my own code. I can never get over not having to define my variables up front, it makes reading other peoples code so difficult, especially since im relatively new to VBA. It takes while if someones using a parameter ive never heard of or just calling out a variable in the middle of the code :rotlaugh:

fumei
12-12-2008, 01:42 PM
"it runs faster as well"

Yes indeed. That is because there is no use of Selection. In particular doing any actual changing to visually see into the header uses GUI resources, and takes time.

Serious VBA coding dealing with headers and footer should NEVER use ActiveWindow.View. From a coding point of view (pun intended) there absolutely no need. The only reason people have View when doing astuff with headers is that they have used the macro recorder.

Macro recording uses Selection. Only Selection.