Results 1 to 8 of 8

Thread: [RESOLVED] working with multiple tables in a word macro

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Resolved [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

  2. #2
    Hyperactive Member
    Join Date
    May 2006
    Posts
    365

    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

  3. #3

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    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

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  5. #5

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    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

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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
    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

  7. #7

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    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

  8. #8

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    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
  •  



Click Here to Expand Forum to Full Width