最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论