PDA

View Full Version : How to remove all footers from a word file.



jlokesh16
05-12-2016, 03:15 AM
How can i remove only footers from selected pages by VBA code.

gmayor
05-12-2016, 06:05 AM
Footer is not a page attribute. There are potentially three footers for each section in the document. Your message title suggests that you want to remove all footers from a document, which is easy enough
Sub removeallfooters()
Dim oSection As Section
Dim oFooter As HeaderFooter
For Each oSection In ActiveDocument.Sections
For Each oFooter In oSection.Footers
If oFooter.Exists Then
oFooter.Range.Text = ""
End If
Next oFooter
Next oSection
lbl_Exit:
Set oSection = Nothing
Set oFooter = Nothing
Exit Sub
End Sub
If yuou want a particular footer to be removed then you need to provide more information.

jlokesh16
05-13-2016, 12:17 AM
I have many types of footers in the document, but specifically there is a footer which is in the following format "ABC 000001, ABC 000002, ABC 000003". so i want these footers to be removed which starts from ABC

gmayor
05-13-2016, 01:02 AM
Whether the number is plain text or a page field, the following should work.


Sub removeallfooters()
Dim oSection As Section
Dim oFooter As HeaderFooter
Dim oRng As Range
Dim bFields As Boolean
bFields = ActiveWindow.View.ShowFieldCodes
ActiveWindow.View.ShowFieldCodes = False
For Each oSection In ActiveDocument.Sections
For Each oFooter In oSection.Footers
If oFooter.Exists Then
Set oRng = oFooter.Range
With oRng.Find
Do While .Execute(FindText:="ABC [0-9]{1,}", _
MatchWildcards:=True)
oFooter.Range.Text = ""
Loop
End With
End If
ActiveWindow.View.ShowFieldCodes = True
Set oRng = oFooter.Range
With oRng.Find
Do While .Execute(FindText:="ABC ^d PAGE")
oFooter.Range.Text = ""
Loop
End With
Next oFooter
Next oSection
ActiveWindow.View.ShowFieldCodes = bFields
lbl_Exit:
Set oSection = Nothing
Set oFooter = Nothing
Set oRng = Nothing
Exit Sub
End Sub