Results 1 to 7 of 7

Thread: Copy tables from documents to another documents with predefined bookmarks

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Thank you very much!

    I have managed to put together a code that copies table from one document and pastes it into antother document into predefined bookmark.

    Option Explicit
    Option Base 1
    
    Sub CopyTablesFromDoc2Doc()
        Dim oSourceDoc As Document
        Dim oTargetDoc As Document
        Dim oRng As Word.Range
        'Optimize Code
        Application.ScreenUpdating = False
        Set oSourceDoc = Documents.Open("C:\Source\001_01_Source_VK.docx")
        Set oTargetDoc = Documents.Open("C:\Target\001_01_Target_VK.docx")
        With oTargetDoc
            Set oRng = .Bookmarks("TabObce").Range
            oRng.FormattedText = oSourceDoc.Tables(1).Range.FormattedText
            .Bookmarks.Add "TabObce", oRng
            Set oRng = .Bookmarks("TabDemografie").Range
            oRng.FormattedText = oSourceDoc.Tables(2).Range.FormattedText
            .Bookmarks.Add "TabDemografie", oRng
        End With
        oSourceDoc.Close
        oTargetDoc.Save
        oTargetDoc.Close
    End Sub
    I have tried to select the tables from the source document by their title using a function but to no avail. Any piece of advice will be much appreciated.

    Sub CopyTablesFromDoc2Doc()
         '....
        With oTargetDoc
            Set oRng = .Bookmarks("TabObce").Range
            Set oSourceTable = getTableByTitle("TabA")
            oRng = oSourceTable.Range
            oRng.FormattedText = oSourceDoc.Tables(1).Range.FormattedText
            '....
    End Sub
    
    Function getTableByTitle(tabTitle As String) As Table
        Dim shape As Variant
        For Each shape In oSourceDoc.Tables
            If shape.Title = tabTitle Then
                getTableByTitle = shape
                Exit Function
            End If
        Next
    End Function
    As a next step I will try to incorporate a loop through the source and target documents.
    Last edited by Aussiebear; 12-22-2024 at 04:17 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •