Create Footer in Word Doc with VBA and Right Align

Viewed 2273

I am creating a word doc using VBA, and I just need to edit the header/footers. When I run the VBA below, the footer is centered... how do I right-align it? Thanks!

With ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).range.Text = DocName
.Headers(wdHeaderFooterPrimary).range.Font.Name = "Arial"
.Headers(wdHeaderFooterPrimary).range.Font.size = 9
.Footers(wdHeaderFooterPrimary).range.Text = Format(Date, "Long Date") 
.Footers(wdHeaderFooterPrimary).range.Font.Name = "Arial"
.Footers(wdHeaderFooterPrimary).range.Font.size = 9
End With
1 Answers

There's more than one way to go about formatting the content of a Footer...

  1. The most "correct" way would be to change the definition of the Footer style. That would include the font formatting that the code in the question is applying directly.

For example:

With ActiveDocument.Styles("Footer")
  .Font.Name = "Arial"
  .Font.Size = 9
  .ParagraphFormat.Alignment = wdAlignParagraphRight
End With
  1. If the formatting should be applied directly, it's possible to simply apply the right alignment directly:

Example based on the code in the question

.Footers(wdHeaderFooterPrimary).range.ParagraphFormat.Alignment = wdAlignParagraphRight
  1. The default Footer style has three tab stops defined: left-aligned, centered and right-aligned. This allows for content to be aligned across the page. In order to use this, leave the paragraph alignment out of (1) or (2) and put two TAB characters (vbTab) at the beginning of the string being written to the Footer:

Example based on the code in the question

.Footers(wdHeaderFooterPrimary).range.Text = vbTab & vbTab & Format(Date, "Long Date") 
Related