Inserting multiple tables into a document

P

Peter

I am developing a VBA program to process a number of tables in a word
document by sorting each table then transferring the contents of each sorted
table to a new table (with changed layout) in another document.

My code successfully sorts the tables in the source document, and adds the
first new table in the new document Ok, but all subsequent new tables appear
to be added as nested within the first table of the new document because my
insertion point appears to remain with the range of the first table.

How do I programmatically change the Range Selection/Insertion point each
time to go to a new point outside the end of the last table inserted so that
when I do a Tables.Add statement it inserts the next table as a completely
new table following the previous one? I have tried adding code:
Selection.MoveDown Unit:=wdLine, Count =1 , but this appears to just
extend the existing range of the current table.
 
J

Jezebel

The trick is to insert a paragraph after each table.

Dim pTable As Word.Table
Dim pIndex As Long
Dim pRange As Word.Range

For pIndex = 1 To 5

Set pRange = ActiveDocument.Range(ActiveDocument.Content.End - 1,
ActiveDocument.Content.End)
Set pTable = ActiveDocument.Tables.Add(Range:=pRange, NumRows:=2,
NumColumns:=4)
ActiveDocument.Content.InsertParagraphAfter

Next
 
P

Peter

Thanks Jezebel your sample code supplied has provided the answer. The key to
it was to include the range variable within each Tables.Add statement which
redefined the range for each table and then allowed a new para to be inserted
after. My code did not correctly define this range when I added each table.

Once again thanks for your assistance.
 

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