none
Inserting Page Numbers into a Word document by sections RRS feed

  • Question

  • I am having difficulty managing page numbers between sections.  I have code creating Word documents with two or three sections depending upon one check box that is selected by the user.  This is working correctly.

    Then when I attempt to insert page numbers in the document footer by each section, the code ignores sections and sets it for the entire document. 

    This is the code:

        'Page numbering.. Add Page Numbers to any sections after section #1
    
        Dim SectionNumber As Integer
        If chkExecSummary.Checked = True Then ' If this checkbox is checked, then the second section should have page numbering as LowercaseRoman
          SectionNumber = 2
          Dim FooterExecSummaryRange As Wordx.Range = wdDoc.Sections(SectionNumber).Footers(Wordx.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
          Dim FooterExecSummarySelection As Wordx.Selection = wdDoc.Sections(SectionNumber).Footers(Wordx.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
          wdDoc.Sections(SectionNumber).PageSetup.DifferentFirstPageHeaderFooter = True
          FooterExecSummaryRange.Fields.Add(FooterExecSummaryRange, , " Page ", False)
          FooterExecSummaryRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight
          FooterExecSummaryRange.Font.Name = "CG Omega"
          FooterExecSummaryRange.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack
          With FooterExecSummarySelection.HeaderFooter.PageNumbers
            .NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleLowercaseRoman
            .HeadingLevelForChapter = 0
            .IncludeChapterNumber = False
            .RestartNumberingAtSection = True
            .StartingNumber = 1
            .ShowFirstPageNumber = True
          End With
          FooterExecSummarySelection.HeaderFooter.LinkToPrevious = Not FooterExecSummarySelection.HeaderFooter.LinkToPrevious
        End If
    
        If chkExecSummary.Checked = True Then SectionNumber = 3 Else SectionNumber = 2 ' Determines if there are two or three sections in word doc.
    
    
        Dim FooterSectionsRange As Wordx.Range = wdDoc.Sections(SectionNumber).Footers(Wordx.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        Dim FooterSectionsSelection As Wordx.Selection = wdDoc.Sections(SectionNumber).Footers(Wordx.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        wdDoc.Sections(SectionNumber).PageSetup.DifferentFirstPageHeaderFooter = True
        FooterSectionsRange.Fields.Add(FooterSectionsRange, , " Page ", False)
        FooterSectionsRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight
        FooterSectionsRange.Font.Name = "CG Omega"
        FooterSectionsRange.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack
        With FooterSectionsSelection.HeaderFooter.PageNumbers
          .NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleArabic
          .HeadingLevelForChapter = 0
          .IncludeChapterNumber = False
          .RestartNumberingAtSection = True
          .StartingNumber = 1
          .ShowFirstPageNumber = True
        End With
        FooterSectionsSelection.HeaderFooter.LinkToPrevious = Not FooterSectionsSelection.HeaderFooter.LinkToPrevious
    

    When there are three sections, it numbers section 1, skips section 2, and numbers section 3.  In a three section doc, it should not number section 1, then number section 2 with lower case roman numerals, and then in section three restart numbering with arabic number 1.

    When there are two sections it numbers some pages in section 1, and all pages in section two with arabic numbers.  In this case, it should only number section two with arabic numbering starting with "1".

    No sections should "link to previous".

    How can I programatically iterate through sections and place page numbers, restarting by section, and be able to set the format of the pagination?

    Any assistance would be greatly appreciated!

    Friday, May 28, 2010 4:23 PM

Answers

  • Hi Roberts,

    With the LinkToPrevious property set to fale, we'll be able to set every individual header/footer, whatever the logic is the header/footer will not impacted by other pages.

    I'm using following VBA code to set Section 4's header for example:

    Sub Macro8()
    Application.ScreenUpdating = False

    ActiveDocument.Sections(3).Range.Select

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

    Selection.HeaderFooter.LinkToPrevious = False
    ActiveDocument.Sections(3).Headers.Item(wdHeaderFooterPrimary).Range.Select
    Selection.Range.Text = "Section3"
    ActiveDocument.Sections(3).Range.Select
    Application.ScreenUpdating = True

    End Sub

    Hope this could give you some ideas.
    Thanks.

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tim Li Tuesday, June 8, 2010 1:53 AM
    Thursday, June 3, 2010 8:22 AM

All replies

  • Hi David,

    After review your post, I think I still could not understand your requirement, I belive this is the reason why you have not received any reply yet.

    I mainly could not understand this part of the question

    "When there are three sections, it numbers section 1, skips section 2, and numbers section 3.  In a three section doc, it should not number section 1, then number section 2 with lower case roman numerals, and then in section three restart numbering with arabic number 1."

    Would you please explain little bit more?

    However, this line of your code:
    FooterSectionsSelection.HeaderFooter.LinkToPrevious = Not FooterSectionsSelection.HeaderFooter.LinkToPrevious

    means if the present setting of LinkToPrevious is set to true then set it to false, if the present setting of LinkToPrevious is set to false than set it to true. But based on my understand of your question, we should alway set LinkToPrevious to false, so it should be:

    FooterSectionsSelection.HeaderFooter.LinkToPrevious =false

    Through above line of code, we could able to set the footer of every page of the document with evey pate has its own footer value.

    Hope this helps.

    Thanks.

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, May 31, 2010 2:44 PM
  • I didn't think about the "NOT" ending up turning the off option back "on" on the third document section.  I will use  =false which is more appropriate.  Thank you.

    To explain further about how the decision to put page numbers in the document by section occurs, the resultant Word document created by VB.Net code always has at least two sections.  The decision to create the extra section is dependent upon a whether a user checks a checkbox on the form indicating the need to add the third section.  So my code checks to determine if the user has checked the checkbox, and then adds a section during the document creation.

    Once the document is completely created I run the paging code that I inserted in my previous note.  

    If the document has two sections:

    • Section 1 - No Page Numbers
    • Section 2 - Page Numbers on Right Hand Side of Page starting with the number "1." (style:arabic) incrementing by page until end of document.

    If the the document has three sections:

    • Section 1: No Page Numbers
    • Section 2: Page Numbers on Right Hand Side of page starting with roman numeral "i" (style:lowercaseroman) incrementing by page until end of section.
    • Section 3: Page Numbers on Right Hand Side of Page starting with the number "1." (style:arabic) incrementing by page until end of document.

    I hope this helps.  Thank you for following up.

    Stu Roberts

    Tuesday, June 1, 2010 7:06 PM
  • Hi Roberts,

    With the LinkToPrevious property set to fale, we'll be able to set every individual header/footer, whatever the logic is the header/footer will not impacted by other pages.

    I'm using following VBA code to set Section 4's header for example:

    Sub Macro8()
    Application.ScreenUpdating = False

    ActiveDocument.Sections(3).Range.Select

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

    Selection.HeaderFooter.LinkToPrevious = False
    ActiveDocument.Sections(3).Headers.Item(wdHeaderFooterPrimary).Range.Select
    Selection.Range.Text = "Section3"
    ActiveDocument.Sections(3).Range.Select
    Application.ScreenUpdating = True

    End Sub

    Hope this could give you some ideas.
    Thanks.

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Tim Li Tuesday, June 8, 2010 1:53 AM
    Thursday, June 3, 2010 8:22 AM
  • Hi,

    I am writing to check the status of the issue on your side. Could you please let me know if the suggestion works for you or not? If you have any questions or concerns, please feel free to let me know. I will be more than happy to be of assistance.

     

    Tim Li

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, June 7, 2010 2:03 AM
  • The suggestion code has been helpful, and is partially working.   The part that is not working is logic in my code, setting the section number.  From a coding standpoint, my problem is resolved.

    Thank you for your help.

    Monday, June 7, 2010 9:15 PM
  • Hi 

    I was not able to get this.

    I am getting an error, Object does not support this property.

    I am accessing a word 2016 document using VBA and i need to track the page number when an some hyperlink is encountered. Please help me with this issue.

    Thursday, January 11, 2018 1:55 PM