none
Word VBA Insert a file as an object from a button RRS feed

  • Question

  • Hi all

    I have no VBA experience at all and am trying to insert an existing table from within another word document from a button in another word document. The purpose is to be able to add in multiples of the same tables from different files just by clicking buttons on the cover/first page of the document to then be able to fill them out in the one document.

    Any help would be greatly appreciated.

    • Edited by 1213456 Friday, April 21, 2017 11:33 AM
    Tuesday, April 18, 2017 5:02 PM

Answers

  • If you want a usable table, you need to insert the table itself. The following example will insert the first table from the named document at the end of the current document. Repeated use will add further copies at the end of the document. 

    Option Explicit
    
    Sub InsertTableAtEnd()
    Dim oSource As Document
    Dim oTarget As Document
    Dim oTable As Range
    Dim oRng As Range
        Set oTarget = ActiveDocument
        Set oRng = oTarget.Range
        oRng.Collapse 0
        Set oSource = Documents.Open _
                      (FileName:="C:\Path\DocumentWithTable.docx", _
                       addtorecentfiles:=False, _
                       Visible:=False)
        Set oTable = oSource.Tables(1).Range
        oRng.FormattedText = oTable.FormattedText
        oRng.End = oRng.End + 1
        oRng.InsertParagraphAfter
        oSource.Close 0
    lbl_Exit:
        Set oSource = Nothing
        Set oTarget = Nothing
        Set oRng = Nothing
        Set oTable = Nothing
        Exit Sub
    End Sub
    



    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by 1213456 Friday, April 21, 2017 9:10 AM
    Wednesday, April 19, 2017 3:57 AM