WORD Footer: VBA

B

Brian Handly

I recorded the following Footer Macro in WORD XP. However, the macro
did not record the formating I wanted applied to the Footer. What do I
need to add to the following Macro so that the text will be displayed in
Times Roman in 8 point?

Texas Handly


================================================
Sub Footer1()
'
' Footer1 Macro
' Macro recorded 11/22/2005 by Brian Handly
'
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
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
NormalTemplate.AutoTextEntries("Filename and path").Insert Where:= _
Selection.Range, RichText:=True
Selection.TypeText Text:=" "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
"SAVEDATE \@ ""M/d/yyyy h:mm am/pm"" ", PreserveFormatting:=True
Selection.TypeText Text:=" "
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
 
J

Jay Freedman

Hi Brian,

By default, all footers in Word have the "Footer" style applied to
them. The best way to set the size and font of the footer text is to
modify the settings in that style. In code, that would be

With ActiveDocument.Styles("Footer").Font
.Name = "Times New Roman"
.Size = 8
End With

On another point, 99% of the code you got from the macro recorder is
pure horse manure. Using the Selection to manipulate headers and
footers is a horrible mess, to be avoided whenever possible. Instead,
use VBA's objects, as illustrated here:

Sub footer2()
Dim oRg As Range

Set oRg = _
Selection.Sections(1).Footers(wdHeaderFooterPrimary).Range

' insert the filename autotext
NormalTemplate.AutoTextEntries("Filename and path").Insert _
Where:=oRg, RichText:=True

' insert a space and then the SaveDate field
With oRg
.Collapse wdCollapseEnd
.Text = " "
.Collapse wdCollapseEnd
.Fields.Add Range:=oRg, Type:=wdFieldEmpty, _
Text:="SAVEDATE \@ ""M/d/yyyy h:mm am/pm"" ", _
PreserveFormatting:=True
End With

' re-select the whole footer and update it
Set oRg = _
Selection.Sections(1).Footers(wdHeaderFooterPrimary).Range
oRg.Fields.Update

' change the Footer style
With ActiveDocument.Styles("Footer").Font
.Name = "Times New Roman"
.Size = 8
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
B

Brian Handly

Jay

Thanks for the help. I am more familiar with the EXCEL VBA objects than
the WORD VBA objects.

Where can I find out what
a) .Collapse wdCollapseEnd does?
b) Why I need to " ' re-select the whole footer and update it"?

Texas Handly
 
J

Jonathan West

Brian Handly said:
Jay

Thanks for the help. I am more familiar with the EXCEL VBA objects than
the WORD VBA objects.

Where can I find out what
a) .Collapse wdCollapseEnd does?

In the VBA editor, position the cursor on "Collapse" and press F1
b) Why I need to " ' re-select the whole footer and update it"?

You need to update the footer in order to get the fields that were inserted
in the autotext entry to show the correcxt values.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jay Freedman

In the VBA editor, position the cursor on "Collapse" and press F1

That's generally good advice. In the VBA editor, F1 displays the help
topic about whatever keyword contains the cursor.
You need to update the footer in order to get the fields that were inserted
in the autotext entry to show the correcxt values.

If the question is more about "why do I need to re-select the whole
footer" rather than "why do I need to update it", the answer is that
at that point in the macro's execution the range oRg covers only the
SaveDate field. So if the range wasn't reassigned to the full footer,
then updating oRg.Fields would miss out the FileName field at the left
end.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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