[RESOLVED] working with multiple tables in a word macro
I'm a little confused about how to work with multiple tables in a word macro.
I dynamically added one table to a word document, and referred to it as Selection.Tables(1), and I was able to populate it dynamically. But then I built a second table and referred to it as Selection.Tables(2), but I got an error message that this member of the collection did not exist.
I then did a Record Macro to see how it was handled. To my surprise, the Macro referenced them all as Selection.Tables(1)! So I went back to my code and did the same thing, but all the data I was populating with all went into the same table and made a big mess. I don't know how to get the second table built and populate it with data.
Help?
Thanks.
Re: working with multiple tables in a word macro
Hello wengang,
Before you try to work with tables in Word using vba please build an array of tables in the document.
The best thing MS did for table arrays is that as soon as you begin working with the object it becomes missing in the array. Well that is the way I think of it. I have posted a function of how I have dealt with vba in Excel to open multiple word documents and identify the tables so as to use the information in each table for an Excel workbook.
Code:
delimiter = Chr$(13) & Chr$(10) 'vbcrlf
Set temp = Worksheets("Template")
Set tbl = wdDoc.Tables(i) 'look at the next table in the series
Set rng = tbl.ConvertToText(vbTab, NestedTables:=False) 'envelope in a word range
str = rng.Text 'pull the text into a variable
wdDoc.Undo 'reset the table object
arrSTR() = Split(str, vbCr) 'separate the table text into separate data items
I cannot emphasise enough the line to reset the table object. If you do not do this the table object is dropped from the word document?
Kind regards
Steve
Re: working with multiple tables in a word macro
Thanks for your help.
Frankly, I'm not following it very well.
I'm building my Word doc in an Access code page.
I don't see where you used delimiter again, so I assume it isn't relevant.
Is wdDoc an object you Dimmed earlier in the code? What object is it, an Word Document? I know in Access, I have to dim a WordApplication to access Word's internal properties.
Why did the index i take you to the next table?
and why did .Undo reset the table object?
I'm not sure if this excerpt addresses my problem. I need to know either how to put a piece of text in any cell of any table on a word doc (given that I know how many tables I created and what dimensions the table are) or why Word does this in a recorded macro by referring to each table as Tables(1), but when I do that in my own code, the data all goes in the same table. Seems like something is happening that is not being recorded in the macro.
As I said, I have successfully created one table and populated it. The problem starts when I build the second table.
Re: working with multiple tables in a word macro
Why not directly set the table and work with it rather then working with the indexes? for example
Code:
Sub Sample()
Dim tbl1 As Table, tbl2 As Table
Set tbl1 = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=3, NumColumns:=4)
'~~> Move to a different location and then use Selection.Range
'~~> Or use a different range alltogether?
Set tbl2 = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=3, NumColumns:=4)
End Sub
Re: working with multiple tables in a word macro
Koolsid
I really thought is should be that simple, but I couldn't find any good sample code, and the record macro wasn't giving me the right answer. It's a project at the office, so I won't be able to play around with it again until tomorrow.
Thanks
Re: working with multiple tables in a word macro
>I really thought is should be that simple
Yes it is :)
Give it a try and if you get stuck simply post back and we will take it from there :)
Re: working with multiple tables in a word macro
Thanks
That does work so much better.
I made the mistake of assuming everything in the recorded macro was necessary to make the table work.
Actually, I doing this in Access, not Word, but I'm dynamically building a Word doc with some tables off some data that changes.
I know there are automations for this, but I wanted the feature on my database form.
There are still some formatting issues to address, but it's mop-up work.
Thanks again!
Re: [RESOLVED] working with multiple tables in a word macro
Just a quick follow-up. I came back to this code to reuse it for something else, and I'd suggest using
Dim Table(20) as Table rather than Tbl1, Tbl2, etc. That will make it much easier if you're doing repetitive work (such as making numerous tables of the same structure).