vbams-word

Adding page number to a section footer in custom format using vba in word document


I want to insert page number in the following format at the centre of my section footer. 1/10, 2/10,... 10/10

I tried the following code, this inserts 1/10 aligned at the left side of the footer.

    Set rngHeader = wdDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
    rngHeader.Delete
    With rngHeader
        '.InsertAfter Text:="Page "

        '.MoveEnd wdCharacter, 0
        .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", PreserveFormatting:=False

  
        .InsertAfter Text:="  / "
        .Collapse wdCollapseStart
        .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
    End With

I am unable to control the insertion of special field. I am new to word vba.

Thanks in advance for the experts.

-- Subbu


Solution

  • Try:

    With wdDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
      .Text = "//"
      .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", PreserveFormatting:=False
      .Fields.Add Range:=.Characters.First, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
      .ParagraphFormat.Alignment = wdAlignParagraphCenter
    End With