VBA Word Footer Page xx of yy

I need to replace all header and footer info in many Word documents.

The new data is entered via a UserForm

My code extract is as follows:

' ------------------------------------------------------------

Private Sub CommandButton1_Click() 

Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oSec As Word.Section

Dim strTitle As String
Dim strVersion As String
Dim strClassification As String
Dim strPageInfo As String

Dim xx as Long

Dim yy As Long

strTitle = TextBox1
strVersion = TextBox2
strCopyright = TextBox3
strPath = TextBox4
strClassification = ComboBox1

ActiveDocument.Repaginate
yy = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
MsgBox yy ' For testing

    For Each oSec In ActiveDocument.Sections  ' Delete all existing header and footer content
        oSec.Headers(wdHeaderFooterEvenPages).Range.Text = ""
        oSec.Headers(wdHeaderFooterFirstPage).Range.Text = ""
        oSec.Headers(wdHeaderFooterPrimary).Range.Text = ""

        oSec.Footers(wdHeaderFooterEvenPages).Range.Text = ""
        oSec.Footers(wdHeaderFooterFirstPage).Range.Text = ""
        oSec.Footers(wdHeaderFooterPrimary).Range.Text = ""
    Next oSec
   
    For Each oSec In ActiveDocument.Sections  ' Write new header and footer content
        oSec.Headers(wdHeaderFooterPrimary).Range.Font.Name = "Arial"
        oSec.Headers(wdHeaderFooterPrimary).Range.Font.Size = "20"
        oSec.Headers(wdHeaderFooterPrimary).Range.Font.Color = wdColorDarkBlue
        oSec.Headers(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
        oSec.Headers(wdHeaderFooterPrimary).Range.Text = strPath
       
        strPageInfo = "Page xx of " & yy
        ' oSec.Footers(wdHeaderFooterPrimary).PageNumbers.Add
       
        oSec.Footers(wdHeaderFooterPrimary).Range.Font.Name = "Arial"
        oSec.Footers(wdHeaderFooterPrimary).Range.Font.Size = "10"
        oSec.Footers(wdHeaderFooterPrimary).Range.Font.Color = wdColorDarkBlue
        oSec.Footers(wdHeaderFooterPrimary).Range.Text = strTitle & vbTab & strVersion & vbTab & strPageInfo & vbCr _
                                                        & strCopyright & vbTab & vbTab & strClassification
 
        oSec.Footers(wdHeaderFooterPrimary).Range.Borders.Enable = True
       
    Next oSec

Unload Me
End Sub
' ------------------------------------------------------------

Everything works OK, except for the "Page xx of yy" part. The value of 'yy' is calculated correctly.

How do I calculate and print the 'xx' part?

Answer
Answer

Graphics in the header/footer are often problematic because floating shapes are located in the first section (in VBA terms) whilst they may appear further down the document.

  For Each aShape In ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes
    aShape.Delete
  Next aShape

You might need similar code for the the other headers in Section 1 if they appear later in the document.

I would be approaching this project by adding content controls to the header and footers of one document and getting the formatting correct. Then save these items to Building Blocks. Then the macro needs to write the UserForm data to the document properties and insert the relevant building blocks to each header and footer in the document.

Andrew Lockton
Melbourne Australia

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Answer
Answer

The following may help clarify. You had some missing variable declarations and I have added a couple. Use Option Explicit at the top of the module to force the declaration of variables - see http://www.gmayor.com/installing_macro.htm   

The following sets the default footer tabs at centre and right margin and addresses all header footers and not just the primary. If you want to change only the primary header footer then change

If oHeader.Exists Then

to

If oHeader.Index = wdHeaderFooterPrimary Then

and

If oFooter.Exists Then

to

If oFooter.Index = wdHeaderFooterPrimary Then

Landscape pages may be an issue - see http://word.mvps.org/FAQs/Formatting/LandscapeSection.htm    Much depends on what you wish to do with them

Private Sub CommandButton1_Click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oSec As Word.Section
Dim oHeader As Word.HeaderFooter
Dim oFooter As Word.HeaderFooter

Dim strTitle As String
Dim strVersion As String
Dim strClassification As String
Dim strCopyright As String
Dim strPath As String
Dim strPageInfo As String
Dim oRng As Range
Dim xx As Long
Dim yy As Long

    strTitle = Me.TextBox1.Text
    strVersion = Me.TextBox2.Text
    strCopyright = Me.TextBox3.Text
    strPath = Me.TextBox4.Text
    strClassification = Me.ComboBox1.Text

    ActiveDocument.Repaginate
    yy = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
    MsgBox yy        ' For testing

    For Each oSec In ActiveDocument.Sections
        For Each oHeader In oSec.Headers
            If oHeader.Exists Then
                Set oRng = oHeader.Range
                With oRng
                    .Font.name = "Arial"
                    .Font.Size = "20"
                    .Font.Color = wdColorDarkBlue
                    .ParagraphFormat.Alignment = wdAlignParagraphRight
                    .Text = strPath
                End With
            End If
        Next oHeader
        For Each oFooter In oSec.Footers
            If oFooter.Exists Then
                Set oRng = oFooter.Range
                With oRng
                    With .ParagraphFormat.TabStops
                        .ClearAll
                        .Add CentimetersToPoints(7.96), wdAlignTabCenter
                        .Add CentimetersToPoints(15.92), wdAlignTabRight
                    End With
                    .Font.name = "Arial"
                    .Font.Size = "10"
                    .Font.Color = wdColorDarkBlue
                    .Text = strTitle & vbTab & strVersion & vbTab & "Page "
                    .Collapse wdCollapseEnd
                    .Fields.Add oRng, wdFieldPage, , False
                    .Start = oFooter.Range.End
                    .Text = " of "
                    .Collapse wdCollapseEnd
                    .Fields.Add oRng, wdFieldNumPages, , False
                    .Start = oFooter.Range.End
                    .Text = vbCr & strCopyright & vbTab & vbTab & strClassification
                    .Borders.Enable = True
                End With
            End If
        Next oFooter
    Next oSec
End Sub

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 1,779 Applies to: