Word 2007 VBA pagenumber alignment problem

R

Rob

I want to use a VB program to add different odd and even headers with
different text and right aligned pagenumber for odd and left alignment for
even. My code inserts the different text for odd/even but the alignment
changes to the last executed alignment. ie- left.


Imports MSW = Microsoft.Office.Interop.Word
Imports WHF = Microsoft.Office.Interop.Word.WdHeaderFooterIndex


With ActiveDocument.Sections(Sct)

.Headers(WHF.wdHeaderFooterPrimary).LinkToPrevious = False
.Headers(WHF.wdHeaderFooterEvenPages).LinkToPrevious = False
.Headers(WHF.wdHeaderFooterFirstPage).LinkToPrevious = False

.Headers(WHF.wdHeaderFooterPrimary).Range.Font.Size = 10
.Headers(WHF.wdHeaderFooterPrimary).Range.Text = "CHAPTER " & Ch
.Headers(WHF.wdHeaderFooterPrimary).Range.Paragraphs.Alignment =
Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter

..Headers(WHF.wdHeaderFooterPrimary).PageNumbers.Add(MSW.WdPageNumberAlignment.wdAlignPageNumberRight, True)
.Headers(WHF.wdHeaderFooterPrimary).PageNumbers.NumberStyle =
Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleArabic

.Headers(WHF.wdHeaderFooterEvenPages).Range.Font.Size = 10
.Headers(WHF.wdHeaderFooterEvenPages).Range.Text = "ACTS OF THE
GENERAL ASSEMBLY"
.Headers(WHF.wdHeaderFooterEvenPages).Range.Paragraphs.Alignment
= Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter

..Headers(WHF.wdHeaderFooterEvenPages).PageNumbers.Add(MSW.WdPageNumberAlignment.wdAlignPageNumberLeft, True)
.Headers(WHF.wdHeaderFooterEvenPages).PageNumbers.NumberStyle =
Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleArabic

End With

Is this a bug or am I doing something wrong. I have tried many different
ways of doing this.
 
G

Graham Mayor

Personally I would either rely on the fact that the default header style has
tabs set at centre and right or probably better still I would clear then add
tabs at centre and right. Then rather than use the insert page numbers with
the frames that seem to have a kind of their own, I would insert page number
fields tabs and text as appropriate. From Word vba that would be as follows.
Your Ch (chapter number wasn't defined) so I substituted the section number
for testing. This works from Word 2007and may help point a way.:

Dim Sct As Long
Dim oRng As Range
For Sct = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(Sct)
.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
With .Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
Set oRng = .Range
With oRng
.ParagraphFormat.TabStops.ClearAll
.ParagraphFormat.TabStops.Add _
Position:=CentimetersToPoints(7.96), _
Alignment:=wdAlignTabCenter, _
Leader:=wdTabLeaderSpaces
.ParagraphFormat.TabStops.Add _
Position:=CentimetersToPoints(15.92), _
Alignment:=wdAlignTabRight, _
Leader:=wdTabLeaderSpaces
.Font.Size = 10
.Text = vbTab & "CHAPTER " & Sct & vbTab
.Collapse wdCollapseEnd
.Fields.Add oRng, wdFieldPage, , False
End With
End With
With .Headers(wdHeaderFooterEvenPages)
.LinkToPrevious = False
Set oRng = .Range
With oRng
.ParagraphFormat.TabStops.ClearAll
.ParagraphFormat.TabStops.Add _
Position:=CentimetersToPoints(7.96), _
Alignment:=wdAlignTabCenter, _
Leader:=wdTabLeaderSpaces
.ParagraphFormat.TabStops.Add _
Position:=CentimetersToPoints(15.92), _
Alignment:=wdAlignTabRight, _
Leader:=wdTabLeaderSpaces
.Font.Size = 10
.Text = vbTab & "ACTS OF THE GENERAL ASSEMBLY" & vbTab
.Collapse wdCollapseStart
.Fields.Add oRng, wdFieldPage, , False
End With
End With
End With
Next Sct


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Rob

Thanks. I have resorted to using something similiar, it would be so nice if
this worked as "I think it should ".

again Thanks !
 

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