none
Inserting a page break between two tables being inserted as objects from word documents RRS feed

  • Question

  • Hi

    I'm looking to insert two (or more) tables into the same document from a button. I can insert them, however I need a pagebreak between them both and currently the tables are butted up against each other. I've tried the following code but it just ends up with an error. 

    Any help would be greatly appreciated.

    Private Sub CommandButton1_Click()

    Dim oSource As Document
    Dim oTarget As Document
    Dim oTable As Range
    Dim oRng As Range
        
        With CreateObject("Word.Document")
            .Windows(1).Visible = True
         
        Set oTarget = ActiveDocument
        Set oRng = oTarget.Range
        oRng.Collapse 0
        Set oSource = Documents.Open _
                      (FileName:="c:\a.doc", _
                       addtorecentfiles:=False, _
                       Visible:=False)
        Set oTable = oSource.Tables(1).Range
        oRng.FormattedText = oTable.FormattedText
        
        oRng.InsertBreak Type:=7 ' wdPageBreak
        
        oRng.End = oRng.End + 1
        oSource.Close 0
        
      
        
        Set oTarget = ActiveDocument
        Set oRng = oTarget.Range
        oRng.Collapse 0
        
        Set oSource = Documents.Open _
                      (FileName:="c:\dc.doc", _
                       addtorecentfiles:=False, _
                       Visible:=False)
        Set oTable = oSource.Tables(1).Range
        oRng.FormattedText = oTable.FormattedText
        oRng.End = oRng.End + 1
        
        oSource.Close 0
        
        End With
        
        
    lbl_Exit:

        Set oSource = Nothing
        Set oTarget = Nothing
        Set oRng = Nothing
        Set oTable = Nothing
        Exit Sub

    End Sub

    Friday, April 21, 2017 9:18 AM

Answers

  • The following code inserts a page break at the end each table.

    Set oDoc = ActiveDocument oDoc.Bookmarks("\EndOfDoc").Range.InsertBreak Type:=wdSectionBreakNextPage With oDoc.Sections.Last For j = 1 To 3 .Headers(j).LinkToPrevious = False .Footers(j).LinkToPrevious = False Next j End With

    • Marked as answer by 1213456 Friday, April 21, 2017 9:58 AM
    Friday, April 21, 2017 9:58 AM