Programmatically Setting Word Footer

C

Carlton

Here's the problem: I have 2,000+ Word documents that I need to change the
cover sheet and footer (on each section) for each. By and large, no problem.
But I can't get the footer to look like the 'Director' wants it to look. The
footer for each section consists of three components: a horizontal line
across the dimension of the footer; a centered text in red (as in 'Top
Secret'), and a page number in the lower-right corner. I can get all three
components on the footer, but not in the right order. The page number is the
upper-most and, since it's added last it pushes the horizontal line down. (If
I add it first, the horizonatl line zaps the page number). So my questions:

1. Can I reposition the page number to the third line of the footer?
2. Or, can I add a horizontal line above the footer to each page (as part of
the 'Pages' collection instead of the footer?

BTW, I've developed the code in VBA using Access and a Word Application. But
even when I run the code from VBA Word I get the same disordered results.

Any advise or assistance will be appreciated. And I hope this makes sense.
 
J

Jay Freedman

In the absence of a sample of your code, I suspect you're using something
like Selection.InsertAfter to "type" the elements into the footer. In
general, that's not the best approach.

For maximum control of positions, I recommend inserting a borderless 1x3
table in the footer. The horizontal line at the top of the footer can be
achieved by turning on the table's top border. The left cell of the table
remains empty. The middle cell is given Centered alignment, and the text
that's put into it can be formatted as desired. The rightmost cell is given
Right alignment, the {PAGE} field is inserted in it, and its paragraph is
given whatever Space Before setting is needed to get the proper vertical
position.

Here's sample code:

Sub MakeFooter()
Dim oSec As Section
Dim oTbl As Table
Dim oRg As Range
' assume all sections have only a Primary footer

For Each oSec In ActiveDocument.Sections
Set oTbl = ActiveDocument.Tables.Add( _
Range:=oSec.Footers(wdHeaderFooterPrimary).Range, _
numrows:=1, numcolumns:=3)
With oTbl
' display only top border
With .Borders
.Item(wdBorderTop).Visible = True
'.Item(wdBorderTop).Color = wdColorBlack
.Item(wdBorderLeft).Visible = False
.Item(wdBorderRight).Visible = False
.Item(wdBorderBottom).Visible = False
End With

' set up the middle cell
Set oRg = .Cell(1, 2).Range
oRg.MoveEnd wdCharacter, -1 ' exclude cell marker
oRg.Paragraphs(1).Alignment = wdAlignParagraphCenter
oRg.Text = "Top Secret"
oRg.Font.Color = wdColorRed

' set up the right cell
Set oRg = .Cell(1, 3).Range
oRg.MoveEnd wdCharacter, -1 ' exclude cell marker
ActiveDocument.Fields.Add Range:=oRg, Type:=wdFieldPage
oRg.Paragraphs(1).Alignment = wdAlignParagraphRight
oRg.Paragraphs(1).SpaceBefore = 28 ' 2 lines in points
End With
Next oSec
End Sub

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

Carlton

Jay:
Great Advice!!
I can't wait to try it. I have no doubt it will work...
Carlton
 

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