|
-
May 31st, 2013, 03:45 PM
#1
Thread Starter
Frenzied Member
[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.
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
May 31st, 2013, 04:26 PM
#2
Hyperactive Member
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
-
Jun 1st, 2013, 08:50 PM
#3
Thread Starter
Frenzied Member
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.
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Jun 2nd, 2013, 01:52 AM
#4
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jun 2nd, 2013, 04:27 PM
#5
Thread Starter
Frenzied Member
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
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Jun 2nd, 2013, 11:54 PM
#6
-
Jun 3rd, 2013, 07:48 AM
#7
Thread Starter
Frenzied Member
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!
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Jul 24th, 2015, 01:17 PM
#8
Thread Starter
Frenzied Member
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).
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|