VBA - footer

j4ymf

Well-known Member
Joined
Apr 28, 2003
Messages
776
Office Version
  1. 365
Platform
  1. Windows
Hello

We use this VBA code in our footer where it exports to a word document, but is there a way i can add a page number aswell?

VBA Code:
    ' Add text to footer in left-aligned
    footerText = " Company info "

    ' Add footer to the document
    With wddoc.Sections(1).Footers(1).Range ' wdHeaderFooterPrimary = 1
        .Text = footerText
        .ParagraphFormat.Alignment = wdAlignParagraphLeft ' Left-align the footer
        .Font.Name = "Century Gothic"
        .Font.Size = 10
        .Font.Color = RGB(56, 56, 56) ' Dark grey/black color
        ' Removed the left indent for no spacing
        .ParagraphFormat.SpaceAfter = 1 ' No space after the footer
        .ParagraphFormat.SpaceBefore = 0 ' No space before the footer
    End With
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You can record a macro in Word to add the page number then you'll have the code you can add to your present code.
 
Upvote 0
Try adding the following code after End With:
VBA Code:
    Set frng = wddoc.Sections(1).Footers(1).Range
    frng.InsertAfter vbTab & vbTab & "Page "
    frng.Collapse Direction:=wdCollapseEnd
    frng.Fields.Add Range:=frng, Type:=wdFieldPage, PreserveFormatting:=True
    Set frng = wddoc.Sections(1).Footers(1).Range
    frng.InsertAfter " of "
    frng.Collapse Direction:=wdCollapseEnd
    frng.Fields.Add Range:=frng, Type:=wdFieldNumPages, PreserveFormatting:=True
 
Upvote 0
Try adding the following code after End With:
VBA Code:
    Set frng = wddoc.Sections(1).Footers(1).Range
    frng.InsertAfter vbTab & vbTab & "Page "
    frng.Collapse Direction:=wdCollapseEnd
    frng.Fields.Add Range:=frng, Type:=wdFieldPage, PreserveFormatting:=True
    Set frng = wddoc.Sections(1).Footers(1).Range
    frng.InsertAfter " of "
    frng.Collapse Direction:=wdCollapseEnd
    frng.Fields.Add Range:=frng, Type:=wdFieldNumPages, PreserveFormatting:=True
Hello Tetra201

Thank you for coming back to me over the festive period.
the macro runs and fails, it gets as far as adding in the word "Page" to the footer but fails on this line.

any ideas

1735203220984.png


1735203308073.png
 
Upvote 0
Thank you for the feedback.

Where from are you running the code? -- Excel? Word? Something else?
Are there any error messages when the macro fails?
 
Upvote 0
For example:
VBA Code:
Const footerText As String = " Company info "
Const wdHeaderFooterPrimary As Long = 1: Const wdFieldEmpty As Long = -1
Const wdCollapseStart As Long = 1: Const wdAlignParagraphLeft As Long = 0
Const wdAlignTabRight As Long = 2: Const wdTabLeaderSpaces As Long = 0
With wddoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
    .Font.Name = "Century Gothic"
    .Font.Size = 10
    .Fields.Add .Duplicate, wdFieldEmpty, "NUMPAGES", False
    .Collapse wdCollapseStart
    .Text = " of "
    .Collapse wdCollapseStart
    .Fields.Add .Duplicate, wdFieldEmpty, "PAGE", False
    .Collapse wdCollapseStart
    .Text = vbTab & "Page "
    .ParagraphFormat.Alignment = wdAlignParagraphLeft
    .ParagraphFormat.TabStops.Add Position:=InchesToPoints(6), Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
    .ParagraphFormat.SpaceAfter = 0
    .ParagraphFormat.SpaceBefore = 0
    .Collapse wdCollapseStart
    .Text = footerText
    .Font.Color = RGB(56, 56, 56) ' Dark grey/black color
End With
 
Upvote 0
Hi Macropod

thanks for the reply
I get this Compile error:

any ideas?

1735307028865.png
 
Upvote 0
Hi Tetra201
I get a run time error

thank for your help

1735307205752.png
1735307222412.png
 
Upvote 0
Try changing:
Position:=InchesToPoints(6)
to:
Position:=wdapp.InchesToPoints(6)
where 'wdapp' is whatever you're calling the Word application.
 
Upvote 0
Solution
For my suggestion (Post #3), try changing all named constants to their values like this:
VBA Code:
    Set frng = wddoc.Sections(1).Footers(1).Range
    frng.InsertAfter vbTab & vbTab & vbTab & vbTab & "Page "
    frng.Collapse Direction:=0
    frng.Fields.Add Range:=frng, Type:=-1, Text:="PAGE  ", PreserveFormatting:=True
    Set frng = wddoc.Sections(1).Footers(1).Range
    frng.InsertAfter " of "
    frng.Collapse Direction:=0
    frng.Fields.Add Range:=frng, Type:=-1, Text:="NUMPAGES  ", PreserveFormatting:=True
 
Upvote 0

Forum statistics

Threads
1,225,345
Messages
6,184,394
Members
453,229
Latest member
Piip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top