Word VBA: Copy tables from one document to another.

The code below does copy the tables to a new document, but they end up as nested tables.  In the original document, there are 3 paragraphs between the two tables.  How can I unselect the first table after I post it in the new document, add a paragraph after the table, and then paste the second table?

Sub ExtractTablesFromOneDoc()
  Dim objTable As Table
  Dim objDoc As Document
  Dim objNewDoc As Document
  Dim objRange As Range
  Dim o As Paragraph
 
  Set objDoc = ActiveDocument
  Set objNewDoc = Documents.Add
 
  For Each objTable In objDoc.Tables
    objTable.Range.Select
    Debug.Print objTable.Title
    Selection.Copy
 
    '  Paste tables to new document in rich text format.
    Set objRange = objNewDoc.Range
    objRange.Collapse Direction:=wdCollapseEnd
    objRange.PasteSpecial DataType:=wdPasteRTF
    objRange.Collapse Direction:=wdCollapseEnd
  Next objTable
 
End Sub

Open in new window

rrhandle8Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Add     objRange.InsertParagraphAfter before the Next.
 
  For Each objTable In objDoc.Tables
    objTable.Range.Select
    Debug.Print objTable.Title
    Selection.Copy
 
    '  Paste tables to new document in rich text format.
    Set objRange = objNewDoc.Range
    objRange.Collapse Direction:=wdCollapseEnd
    objRange.PasteSpecial DataType:=wdPasteRTF
    objRange.Collapse Direction:=wdCollapseEnd

    objRange.InsertParagraphAfter

  Next objTable

Open in new window

0
rrhandle8Author Commented:
Norie, that did not work.
0
NorieAnalyst Assistant Commented:
It definitely works for me, how exactly are the tables in the original document arranged?

Could you attach a sample document?
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

rrhandle8Author Commented:
Table1.docx has the two tables
Table_Controller.docm has the code.
Table1.docx
Table_Controller.docm
0
NorieAnalyst Assistant Commented:
What I suggested does seem to be pasting the table separately to the new document but it appears to be placing them on top of each other.

I've tried a few things but the only way I've managed to separate the tables in the new document is to insert a page break between them, which I don' think is ideal.

How do you want the copied tables to appear in the new document?
0
rrhandle8Author Commented:
With a paragraph between them.
0
Rgonzo1971Commented:
Hi,

pls try
Sub ExtractTablesFromOneDoc()
  Dim objTable As Table
  Dim objDoc As Document
  Dim objNewDoc As Document
  Dim objRange As Range
  Dim o As Paragraph
 
  Set objDoc = ActiveDocument
  Set objNewDoc = Documents.Add
 
  For Each objTable In objDoc.Tables
    objTable.Range.Select
    Debug.Print objTable.Title
    Selection.Copy
 
    '  Paste tables to new document in rich text format.
    Set objRange = objNewDoc.Range
    objRange.Collapse Direction:=wdCollapseEnd
    objRange.PasteSpecial DataType:=wdPasteRTF
    objRange.Tables(1).Rows.WrapAroundText = False
    objRange.Collapse Direction:=wdCollapseEnd

    objRange.InsertParagraphAfter
  Next objTable
' optional
'  For Each objTable In objNewDoc.Tables
'    objTable.Rows.WrapAroundText = True
'  Next objTable
End Sub

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rrhandle8Author Commented:
Rgonzo1971, that worked.  One note, the document with the original tables has to be the active document.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.