Copy Multiple Tables to Clipboard

C

C_P

I am currently working on a project where I would like to copy multiple
tables from one word document to another word document. The problem
appears when in trying to copy multiple tables to the clipboard - I am
unsure of how to do this. Using the DataObject, I can copy text (from
what I can understand), but I need to keep the actual format of the
table.

I am able to copy one table from the first document to the next, but
when I try to switch back to the first (using
ActiveWindow.Next.Activate or ActiveWindow.Previous.Activate), it will
not switch back. Therefore, I would like to copy all the tables into
the clipboard (if possible).

Please advise.
 
D

Dave Lett

Hi C_P,

I would argue against switching back and forth between documents. Instead, I
think you can accomplish your goals by setting document objects and ranges
and using those to copy your tables from one document to the next. Have a
look at the following sample:

''' document containing the tables
Dim oDocTbl As Document
''' documet to copy tables to
Dim oDocInsert As Document
Dim lTbl As Long
Dim oRngTbl As Range
Dim oRngInsert As Range

'''these documents must be open
'''Set oDocTbl = Documents("DocTables.doc")
'''Set oDocInsert = Document("DocInsert.doc")

'''if they aren't open, then use something like the following
Set oDocTbl = Documents.Open(FileName:="C:\DocTables.doc")
Set oDocInsert = Documents.Open(FileName:="C:\DocInsert.doc")

'''cycle through the tables that you want to copy
For lTbl = 1 To oDocTbl.Tables.Count
Set oRngTbl = oDocTbl.Tables(lTbl).Range
Set oRngInsert = oDocInsert.Range
With oRngInsert
.Collapse Direction:=wdCollapseEnd
'''insert the tables into the other document
'''they will be inserted at the end for this
'''demonstration
.FormattedText = oRngTbl.FormattedText
.Collapse Direction:=wdCollapseEnd
.InsertAfter vbCrLf
.Select
End With
Next lTbl

'''close and save each document
oDocTbl.Close SaveChanges:=wdSaveChanges
oDocInsert.Close SaveChanges:=wdSaveChanges

Set oDocTbl = Nothing
Set oDocInsert = Nothing

HTH,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top