Consulting

Results 1 to 3 of 3

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

  1. #1

    Copy tables from documents to another documents with predefined bookmarks

    Hello all,


    I am looking for some assistance in a matter, not sure if best to do it in Excel or Word VBA; so I'll start with Word VBA.

    I have an application that generates Word documents each generated Word document contains named tables (TableA, TableB, ...). Id like to copy all tables from these generated Word documents to existing documents IN predefined bookmark locations (named BookmarkA, BookmarkB, ...), i.e. TableA in BookmarkA, TableB in BookamrkB, etc.

    For each generated Word document there is one existing Word document. So Id like to copy all tables from the generated document X to the existing document X, all tables from the generated document Y to the existing document Y, all tables from the generated document Z to the existing document Z, etc

    Number of generated Word documents contain 4 tables (either TableA, TableB, TableC, TableE or TableA, TableB, TableD, TableE) and their corresponding existing documents contain 4 bookmarks (either BookmarkA, BookmarkB, BookmarkC, BookmarkE or BookmarkA, BookmarkB, BookmarkD, BookmarkE).
    Most of generated Word documents contain 5 tables (TableA, TableB, TableC, TableD, TableE) and their corresponding target documents contain 5 bookmarks (BookmarkA, BookmarkB, BookmarkC, BookmarkD, BookmarkE).

    Now as this is going to be somewhat repetitive task the content of the bookmarks in the existing documents need to be cleared first before copying the tables from the generated documents.
    The generated (table source) documents are located in a different folder than the existing (target) documents. The corresponding source and target documents can be matched by the first 6 characters of a filename. I was wondering whether a list of matching source documents (column A) and target documents (column B) in an Excel would make thing easier.


    I am new to VBA. I have searched for a similar problem to get me started but havent found any.
    Any help or advice would be much appreciated.


    Pearce

  2. #2
    I have tried to put together a code that would delete the contents of bookmarks using info from several VBA forums. It seems that being a new memeber the forum won't let me to post links to the sources in my message.

    So far the code works only with a plain text - it deletes the text inside the bookmarks.

    Next I will try to modify the code so that it would delete bookmarked tables using info from: rlbcontractor.com/vba-deleting-a-bookmarked-table-without-deleting-the-bookmark


    Code:
    Option Explicit
    
    Sub ClearContentsOfBookmarks()
    Application.ScreenUpdating = False
    
    Dim strFolder As String
    Dim strFile As String
    Dim strName As String
    Dim wdDoc As Document
    Dim i As Long
    
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    strFile = Dir(strFolder & "\*.docx", vbNormal)
    
    While strFile <> ""
        Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
        wdDoc.Activate
        With wdDoc
            For i = 1 To ActiveDocument.Bookmarks.Count
                strName = ActiveDocument.Bookmarks(i).Name
                FillBM strName, ""
            Next i
        End With
        wdDoc.Close SaveChanges:=True
        strFile = Dir()
    Wend
    Set wdDoc = Nothing
    Application.ScreenUpdating = False
    End Sub
    
    Private Sub FillBM(strBMName As String, strValue As String)
    'Graham Mayor
    Dim oRng As Range
        With ActiveDocument
            On Error GoTo lbl_Exit
            Set oRng = .Bookmarks(strBMName).Range
            oRng.Text = strValue
            oRng.Bookmarks.Add strBMName
        End With
    lbl_Exit:
        Set oRng = Nothing
        Exit Sub
    End Sub
    
    Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function

  3. #3
    The following should help

    Sub Macro1()Dim oRng As Range
        Set oRng = ActiveDocument.Tables(1).Range
        FillBM_with_Table "BM1", oRng
        Set oRng = Nothing
    End Sub
    
    
    Private Sub FillBM_with_Table(strbmName As String, oSource As Range)
    'Graham Mayor - http://www.gmayor.com
    Dim oRng As Range
        With ActiveDocument
            On Error GoTo lbl_Exit
            Set oRng = .Bookmarks(strbmName).Range
            If oRng.Tables.Count > 0 Then oRng.Tables(1).Delete
            oRng.FormattedText = oSource.FormattedText
            oRng.Bookmarks.Add strbmName
        End With
    lbl_Exit:
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

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
  •