VBA Automation: Add page number to header (1 Viewer)

besuchanko

New member
Local time
Today, 09:38
Joined
May 14, 2015
Messages
3
I'm using VBA to insert the page number in a header. It's working, exept for a couple of things. I want it to insert the page number at the current position, and restart numbering at each section. I increment the section # with the variable x. Currently, it's inserting the page number at top-left of header. I want it to insert the page number after the word "Page" followed by 2 lines breaks.

Here's snippet of my code:

'Create Header
With ActiveDocument.Sections(x)
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
.PageSetup.DifferentFirstPageHeaderFooter = True
.Headers(wdHeaderFooterPrimary).Range.Font.Size = 9
.Headers(wdHeaderFooterPrimary).Range.Text = "Session: [" & rst!SessionNum & "] " & rst!SessionTitle & _
Chr(13) & "Presenter: " & rst!Full_Name & Chr(13) & "Page " & Chr(13) & Chr(13)
.Headers(wdHeaderFooterPrimary).PageNumbers.Add pagenumberalignment:=wdAlignPageNumberLeft, FirstPage:=True
End With
 

vbaInet

AWF VIP
Local time
Today, 17:38
Joined
Jan 22, 2010
Messages
26,374
besuchanko,

Again, I'm not into VBA for Word but here's how you can insert the page numbers in the way you want:
Code:
    x = 1
    
    ' Alter header and create new page
    With ActiveDocument.Sections(x).Headers(wdHeaderFooterPrimary)
        .LinkToPrevious = False
        .Range.Font.Size = 9
        .Range.Fields.Add .Range, wdFieldPage, , False   ' insert page number as a field
        
        ' Insert text before and after page number field because you can't easily do this with fields
        .Range.InsertBefore "Session: [" & rst!SessionNum & "] " & rst!SessionTitle & Chr(13) & _
                            "Presenter: " & rst!Full_Name & Chr(13) & _
                            "Page "
        .Range.InsertAfter Chr(13) & Chr(13)
        .Parent.Range.InsertBreak Type:=wdSectionBreakNextPage  ' inserts a new page section
    End With
    
    rst.MoveNext
    x = x + 1
That should give you some ideas.
 

marlan

Registered User.
Local time
Today, 19:38
Joined
Jan 19, 2010
Messages
409

besuchanko

New member
Local time
Today, 09:38
Joined
May 14, 2015
Messages
3
Thank you so much for your help. It totally worked. Since I already had section breaks elsewhere in the code, I modified it to the following, which gave me exactly what I needed:

'Create Header with page numbers
With ActiveDocument.Sections(x).Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.Font.Size = 9
.PageNumbers.RestartNumberingAtSection = True
.PageNumbers.StartingNumber = 1
.Range.Fields.Add .Range, wdFieldPage, , False ' insert page number as a field

' Insert text before and after page number field because you can't easily do this with fields
.Range.InsertBefore "Session: [" & rst!SessionNum & "] " & rst!SessionTitle & Chr(13) & _
"Presenter: " & rst!Full_Name & Chr(13) & _
"Page "
.Range.InsertAfter Chr(13) & Chr(13)
End With
 

Users who are viewing this thread

Top Bottom