locked
Why doesn't my code generate the correct footer? RRS feed

  • Question

  • With this code

    HeaderFooter _footer = sections[3].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
    _footer.PageNumbers.RestartNumberingAtSection = true;
    _footer.PageNumbers.StartingNumber = 1;
    object CurrentPage = WdFieldType.wdFieldPage;
    object TotalPages = WdFieldType.wdFieldSectionPages;
    Range footerRange = _footer.Range;
    footerRange.Text = "Line1\t";
    footerRange.Collapse(WdCollapseDirection.wdCollapseEnd);
    footerRange.Fields.Add(footerRange, ref CurrentPage, Nothing, true);
    footerRange.InsertAfter(" / ");
    footerRange.Collapse(WdCollapseDirection.wdCollapseEnd);
    footerRange.Fields.Add(footerRange, ref TotalPages, Nothing, true);
    footerRange.InsertParagraph();
    footerRange.InsertAfter("Line2");

    I cannot post pictures now. So I post the pictures URLs here.

    https://i.stack.imgur.com/6uwXQ.png
    https://i.stack.imgur.com/BH982.png

    Please notify that the current page num “1” and the section pages number “2” appear at the end of the footer.

    I have tried the following two macro codes. They all work very well.

    Selection.TypeText Text:="line1" & vbTab
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "PAGE  ", PreserveFormatting:=True
    Selection.TypeText Text:="/"
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
    "SECTIONPAGES  ", PreserveFormatting:=True
    Selection.TypeParagraph
    Selection.TypeText Text:="line2"
    Selection.TypeText Text:="line1" & vbTab
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage, Text:= _
        "", PreserveFormatting:=True
    Selection.TypeText Text:=" / "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldSectionPages, Text:= _
        "", PreserveFormatting:=True
    Selection.TypeParagraph
    Selection.TypeText Text:="line2"


    I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.


    • Edited by Kun Lin Saturday, September 12, 2020 3:52 PM
    Saturday, September 12, 2020 3:50 PM

All replies

  • Also have same issue!
    Did you resolve it?
    Saturday, September 12, 2020 6:53 PM
  • No. I have search on net for many days. But I haven't resolve it.
    Sunday, September 13, 2020 1:11 AM