Monday, January 12, 2015

Automatic Footers in Word using VBA Macros

I'm in a new office environment in which I'm generating lots of data reports.  One of the things I have found frustrating in other environments is being handed a printed copy of a report and having no idea where the original electronic version is stored. 

This VBA macro can be used to set up a standard footer in your Word documents which includes the full path and filename for the report as well as the current page out of total pages.

View of document showing footer containing filename and page number

Now each time I create a report, I have a simple macro to run which creates a standard footer which shows others where to find the electronic version of my printed reports.


Here is the code
Sub InsertFooter()
' InsertFooter Macro
' Insert a File Footer with filename and page numbers

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
End If
'Set the footer
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.Font.Size = 9
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="FILENAME  \p ", PreserveFormatting:=True
Selection.TypeText Text:=vbTab
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PAGE  ", PreserveFormatting:=True
Selection.TypeText Text:=" of "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="NUMPAGES  ", PreserveFormatting:=True
End Sub

No comments: