This Excel code adapted from
snb's code uses the bookmark and table names in:
TableArray = Array("Table6", "Table7")
BookmarkArray = Array("BM1", "BM2")
(of course, adjust these to suit)
to copy Excel tables to Word.
The Word bookmarks I experimented with were
placeholder bookmarks.
One line that I can't get to work consistently is:
.Bookmarks(BookmarkArray(x)).Range.tables(1).autofitbehavior (2)
so it's commented-out.
It works for one table but not the other; it turns out that one bookmark was right at the end of a line (after some text) before the carriage return and the other was similarly placed but there was nothing else on that line bar a carriage return. Why should that matter?!
The block of commented-out code following that line was me trying to address the problem. It's not just an autofit problem, it's really trying to get a reliable reference to the pasted table to be able to manipulate it.
I'm not familiar with the Word object model so I might post a question on a Word vba forum.
Sub blah()
TableArray = Array("Table6", "Table7")
BookmarkArray = Array("BM1", "BM2")
'With GetObject("C:\Users\Public\Documents\vbaExpress53814\ReceivingDocument.docx")
Set wdoc = GetObject("C:\Users\Public\Documents\vbaExpress53814\ReceivingDocument.docx")
With wdoc
.Parent.Visible = True
For x = LBound(TableArray) To UBound(TableArray)
For Each sh In Sheets
For Each lob In sh.ListObjects
If TableArray(x) = lob.Name Then
'Copy Table Range from Excel
lob.Range.Copy
'Paste Table into MS Word (using inserted Bookmarks -> ctrl+shift+F5)
.Bookmarks(BookmarkArray(x)).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
'.Bookmarks(BookmarkArray(x)).Range.tables(1).autofitbehavior (2)
'This block of comments is my trying to get the line above (autofitbehavior) to work with both pasted tables:
'Set BMRange = .Bookmarks(BookmarkArray(x)).Range
'BMRange.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
'.Bookmarks.Add BookmarkArray(x), BMRange
'.Bookmarks(BookmarkArray(x)).Range.Select 'autofitbehavior (2)
End If
Next lob
Next sh
Next x
End With 'wdoc
End Sub
The above addresses your original question rather than working through a list of documents and tables in a worksheet. I'll try and look at that later.