none
restart page numbering for an inserted document RRS feed

  • Question

  • Hi,

    I am trying to insert word documents, and then add a section to restart page numbering for each newly inserted document.  However, the page numbers do not restart, and the first document has no page numbers at all:

      m_WordApp.ActiveDocument.Repaginate()
                    'If Not fileArray.Contains(filepath) Then
                    Dim oSection = m_WordApp.ActiveDocument.Sections.Add()

                    oSection.PageSetup.DifferentFirstPageHeaderFooter = False
                    oSection.Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).LinkToPrevious = False
                    oSection.Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).PageNumbers.Add(WdPageNumberAlignment.wdAlignPageNumberCenter)

                   
                    m_WordApp.Selection.InsertFile(filepath, ConfirmConversions:=False, Range:="", Link:=False, Attachment:=False)
                    'm_WordApp.Selection.InsertParagraphAfter()
                    m_WordApp.ActiveDocument.Repaginate()
                    m_WordApp.Selection.InsertBreak(Type:=WdBreakType.wdPageBreak)
                    m_WordApp.Selection.EndKey(Unit:=6)
                    m_WordApp.Selection.Collapse(Direction:=WdCollapseDirection.wdCollapseEnd)

    The initial formatting is as follows:

     Dim oSection = paragraph1.Range.Sections.Add()
                    oSection.Footers(1).LinkToPrevious = False
                    '               objDoc.Sections(1).Footers(1).PageNumbers.Add(wdAlignPageNumberCenter)
                    oSection.Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).PageNumbers.Add(WdPageNumberAlignment.wdAlignPageNumberCenter)

    Any help would be greatly appreciated

    Thursday, August 8, 2013 4:21 PM

Answers

  • Try this code on a test document and then you can fit in a looping mechanism. In addition to placing the page number in the footer you have to restart the numbering and give it a starting number. You also need to backup from the last paragraph mark in the destination document; otherwise the footers of the inserted document will overwrite the destination footer. You'll see I used a temporary bookmark for this. You might also want to add code that makes sure the headers are also unlinked.

    Finally just an FYI, I did this on a Mac so that's why the path string looks different.

    Sub InsertFilesBySection()
        Dim doc As Word.Document, rng As Word.Range
        Dim i As Long
        Set doc = ActiveDocument
        
        'loop the following code
            Set rng = doc.Bookmarks("\endofdoc").Range
            rng.InsertBreak Word.WdBreakType.wdSectionBreakNextPage
            
            i = doc.Sections.Count
            doc.Sections(i).Footers(wdHeaderFooterPrimary).LinkToPrevious = False
            doc.Sections(i).Footers(wdHeaderFooterPrimary).PageNumbers.Add (Word.WdPageNumberAlignment.wdAlignPageNumberCenter)
            doc.Sections(i).Footers(wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection = True
            doc.Sections(i).Footers(wdHeaderFooterPrimary).PageNumbers.StartingNumber = 1
            
            rng.Bookmarks.Add "zFileInsert"
            rng.InsertParagraphAfter
            Set rng = rng.Bookmarks("zFileInsert").Range
            rng.insertFile Options.DefaultFilePath(wdDocumentsPath) & ":" & "August_Fundamentals.docx"
            doc.Bookmarks("zFileInsert").Delete
        'end of loop

    End Sub

    Kind Regards, Rich ... http://greatcirclelearning.com

    • Marked as answer by NordicViking Monday, August 12, 2013 6:20 PM
    Friday, August 9, 2013 2:48 AM