none
Macro for Extracting Tables from Word RRS feed

  • Question

  • Hi 

    I am creating a macro to extract tables from one word document and copying them in to a new word document. I have managed to do this using the below:

    Sub CopyTables()
        Dim Source As Document
        Dim Target As Document
        Dim tbl As Table
        Dim tr As Range

        Set Source = ActiveDocument
        Set Target = Documents.Add
              
        For Each tbl In Source.Tables
            Set tr = Target.Range
            tr.Collapse wdCollapseEnd
            tr.FormattedText = tbl.Range.FormattedText
            tr.Collapse wdCollapseEnd
            tr.Text = vbCrLf
        Next
        
    End Sub

     

    However this only extracts the tables in to a new normal word document, where as ideally they would be pasted in to a specially prepared template.

    I have attached just such a template to the word document and by using the below macro i was hoping to be able to extract tables in to the template:

    tmpName = ActiveDocument.AttachedTemplate.FullName 

    Documents.Add Template:=tmpName, NewTemplate:=True

    However although these macros both work independently i am struggling to combine them in a way that works, is someone able to advise?

    kind Regards

    Richard

     
    Thursday, January 28, 2016 2:36 PM

Answers

  • >>>However although these macros both work independently i am struggling to combine them in a way that works, is someone able to advise?

    According to your description, if I didn't misunderstand, you could modify codes like below:

    Sub CopyTables()
         Dim Source As Document
         Dim templateDoc As Document
         Dim tbl As Table
         Dim tr As Range
    
         Set Source = ActiveDocument
         
         Set templateDoc = Documents.Open(FileName:=ActiveDocument.AttachedTemplate.FullName, ReadOnly:=False)
         
         For Each tbl In Source.Tables
             Set tr = templateDoc.Range
             tr.Collapse wdCollapseEnd
             tr.FormattedText = tbl.Range.FormattedText
             tr.Collapse wdCollapseEnd
             tr.Text = vbCrLf
         Next
         
         templateDoc.Save
     End Sub
    

    • Marked as answer by Binstead Friday, January 29, 2016 5:08 PM
    Friday, January 29, 2016 2:46 AM

All replies

  • Use:
    Set Target = Documents.Add(Template:=tmpName)
    where tmpName is a variable holding the path and name of your preferred template.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, January 29, 2016 1:28 AM
  • >>>However although these macros both work independently i am struggling to combine them in a way that works, is someone able to advise?

    According to your description, if I didn't misunderstand, you could modify codes like below:

    Sub CopyTables()
         Dim Source As Document
         Dim templateDoc As Document
         Dim tbl As Table
         Dim tr As Range
    
         Set Source = ActiveDocument
         
         Set templateDoc = Documents.Open(FileName:=ActiveDocument.AttachedTemplate.FullName, ReadOnly:=False)
         
         For Each tbl In Source.Tables
             Set tr = templateDoc.Range
             tr.Collapse wdCollapseEnd
             tr.FormattedText = tbl.Range.FormattedText
             tr.Collapse wdCollapseEnd
             tr.Text = vbCrLf
         Next
         
         templateDoc.Save
     End Sub
    

    • Marked as answer by Binstead Friday, January 29, 2016 5:08 PM
    Friday, January 29, 2016 2:46 AM
  • The OP says he wants to paste the tables into "a specially prepared template".

    Using:
    Set templateDoc = Documents.Open(FileName:=ActiveDocument.AttachedTemplate.FullName, ReadOnly:=False)
    does not do that - it merely opens a new document based on the same template as the active document.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, January 29, 2016 4:05 AM
  • Thank you

    This works for me if i want to select a template that i have saved elsewhere which is good and will help me, in the case above though I had attached a template to the 'active document' as an add-in and was looking to select it in the macro instead of the normal template. 

    Cheers

    Friday, January 29, 2016 5:06 PM
  • Thank you,

    this has done the job; I can now export all tables out of the 'active document' in to a pre-prepared template that is attached to the active document as an Add-in

    Cheers

    Friday, January 29, 2016 5:08 PM