admin管理员组

文章数量:1400731

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

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

Share Improve this question asked Mar 23 at 13:37 SubramanianSubramanian 1011 silver badge12 bronze badges 2
  • "I am unable to control the insertion of special field." is vague. Exactly what problem are you having? Using VBA to insert page numbers seems like the hard way to do this. Why not just use the program interface and add a page number? – John Korchok Commented Mar 23 at 16:30
  • @JohnKorchok - I am adding NumofPages ,"/" & Page in the above code. But its appearing Page, "/" & NumofPages in the section footer. This behaviour I am unable to figure it out – Subramanian Commented Mar 23 at 17:59
Add a comment  | 

1 Answer 1

Reset to default 1

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

本文标签: Adding page number to a section footer in custom format using vba in word documentStack Overflow