PDA

View Full Version : Centering a page number Word 2007



dplaut
08-23-2010, 10:10 AM
I'm putting in a page number with the below code which is directly out of pagenumber help.

Dim MyDoc As Document
Set MyDoc = Application.ActiveDocument
With MyDoc.Sections(1).Footers(wdHeaderFooterPrimary)
.PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberCenter
End With

The problem is that the page number is in a frame and the frame has first line indent = .5 which makes the page number look off center.

How do I change the frame's alignment to be first line indent = 0?

Using Word 2007

gmaxey
08-23-2010, 04:31 PM
Are you sure it is your code that is creating the frame with the indent?

I can insert a page number and force an indent or set the indent to 0, but I don't see how your code produces a frame with an indent.

If you page number isn't competing for real estate in the footer you could simply insert a page field:


Sub ScratchMacroI()
Dim MyDoc As Document
Dim oPN As PageNumber
Dim oFrame As Frame
With ActiveDocument
Set oPN = .Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.Add(PageNumberAlign ment:=wdAlignPageNumberCenter)
Set oFrame = .StoryRanges(wdPrimaryFooterStory).Frames(1)
oPN.Select
If oFrame.Range.InRange(Selection.Range) Then
'oFrame.Range.Paragraphs(1).FirstLineIndent = 36 'I can force a .5 inch indent
oFrame.Range.Paragraphs(1).FirstLineIndent = 0 'or force it to zero
End If
End With
End Sub

Sub ScratchMacroII()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
oRng.Fields.Add oRng.Paragraphs(1).Range, wdFieldPage
oRng.Paragraphs(1).Alignment = wdAlignParagraphCenter
End Sub

dplaut
08-23-2010, 05:19 PM
I'm going to give your technique a try. thanks

My only code is the code in the 1st post. Word is creating the frame.

I discovered that the first line indent is coming from the Normal style of the template (which has first line indent set.) This seems to happen even if I have another style applied to the header or footer.

fumei
08-25-2010, 05:33 PM
"I discovered that the first line indent is coming from the Normal style of the template (which has first line indent set.) This seems to happen even if I have another style applied to the header or footer."

And...a good reason not to use Normal.