Avatar of rrhandle8
rrhandle8
Flag for United States of America asked on

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

VBAMicrosoft Word

Avatar of undefined
Last Comment
rrhandle8

8/22/2022 - Mon
Norie

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

rrhandle8

ASKER
Norie, that did not work.
Norie

It definitely works for me, how exactly are the tables in the original document arranged?

Could you attach a sample document?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
rrhandle8

ASKER
Table1.docx has the two tables
Table_Controller.docm has the code.
Table1.docx
Table_Controller.docm
Norie

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?
rrhandle8

ASKER
With a paragraph between them.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Rgonzo1971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rrhandle8

ASKER
Rgonzo1971, that worked.  One note, the document with the original tables has to be the active document.