none
Limit VBA code to current table RRS feed

  • Question

  • I am trying to use VBA Word 2010 to extract data from one table and insert it into another table inside of Word 2010

    I have written the code to do what I need, however, if there is more then two tables, the VBA code automatically extracts data from the first table in the document.

    How can I restrict VBA Word 2010 code to a specific or current table?

    I am taking complex tables from one application into Word and reformatting them.  Thousands of tables, trying to make this as automatic as possible.

    Everything works as long as I start with only the source table.  But if there is more then one table, it all falls apart.

            

        Dim sT_TestTable As Table
        Set sT_TestTable = ActiveDocument.Tables(1)
        With sT_TestTable
            Dim s0 As Word.Range
            Set s0 = ActiveDocument.Tables(1).Cell(1, 1).Range
            s0.End = s0.End - 1
            Dim s1 As Range
            Set s1 = ActiveDocument.Tables(1).Cell(5, 3).Range
            s1.End = s1.End - 1

    Here is a sample of where I think my problem is.

    Please help.

    thanks.

    • Moved by Hila Shemer Monday, July 23, 2012 8:48 PM The question is related to general Word programmability (not apps for Office) (From:Developing Apps for Office)
    Saturday, July 21, 2012 6:44 AM

Answers

  • Hi ibhammon

    THis forum is for questions concerning a new technology in the preview build of the new version of Office: 2013. Since your question is about VBA in an existing version of Office, you should post in the "Word for Developers" form (http://social.msdn.microsoft.com/Forums/en-US/worddev/threads). Unfortunately, I don't have moderator permissions here, or I'd move it for you.

    FWIW your code specifically tells VBA to access the first table in the document (Tables(1)). If you want to work with a different table, then you have to have a way to identify that table. For example, you could put it in a bookmark. Or, if it follows a specific heading or something else, you could pick up the first table after that point. But you need to know what criterium you can use for your documents.

    FWIW I see something else in your code that you should fix. You set the variable sTTestTable to the table, but then you don't use it in the code following the With block (you always use ActiveDocument.Tables(1)), and you also don't take advantage of the With block (by leaving off ActiveDocument.Tables(1); you could just use .Cell(...)).


    Cindy Meister, VSTO/Word MVP

    Saturday, July 21, 2012 2:36 PM
    Moderator
  • One of the best ways I could find and this isn't necessarily the best answer, but:

    You can do this.

    Let i = ActiveDocument.Tables.Count

    "i" then becomes the number of the last table in the document.

    In my case, I am adding tables to the end of the document, in the order that I want them to appear in the final version.

    I am always adding 1-4 tables at a time that need to be reformatted.

    Because each set of tables is predictable, I know what order they will appear.

    The answer for me was 

    Let i = ActiveDocument.Tables.Count - 1

    I could select the last few tables with -1 or -4.

    When I wanted to select a particular table in my process I would do this.

    Dim pTestTable As Table

    Set pTestTable = ActiveDocument.Tables(i)

    The i then becomes the variable to select the desired table, counting backwards from the last table in the document.

    • Marked as answer by jbhammon Tuesday, July 31, 2012 3:56 PM
    Tuesday, July 31, 2012 3:56 PM

All replies

  • Hi ibhammon

    THis forum is for questions concerning a new technology in the preview build of the new version of Office: 2013. Since your question is about VBA in an existing version of Office, you should post in the "Word for Developers" form (http://social.msdn.microsoft.com/Forums/en-US/worddev/threads). Unfortunately, I don't have moderator permissions here, or I'd move it for you.

    FWIW your code specifically tells VBA to access the first table in the document (Tables(1)). If you want to work with a different table, then you have to have a way to identify that table. For example, you could put it in a bookmark. Or, if it follows a specific heading or something else, you could pick up the first table after that point. But you need to know what criterium you can use for your documents.

    FWIW I see something else in your code that you should fix. You set the variable sTTestTable to the table, but then you don't use it in the code following the With block (you always use ActiveDocument.Tables(1)), and you also don't take advantage of the With block (by leaving off ActiveDocument.Tables(1); you could just use .Cell(...)).


    Cindy Meister, VSTO/Word MVP

    Saturday, July 21, 2012 2:36 PM
    Moderator
  • Thanks Cindy for your reply.  The Word for Developers seemed to intimidating, so I tried this one.  Your answer is exactly what I need.  Bookmarking each table is actually a very good idea and something that I would leverage later on.

    Thanks, Thanks, Thanks

    Saturday, July 21, 2012 3:37 PM
  • If you find Word for Developers too intimidating, you can try the macros section on Answers:
    http://answers.microsoft.com/en-us/office/forum/word

    Cindy Meister, VSTO/Word MVP

    Saturday, July 21, 2012 5:20 PM
    Moderator
  • One of the best ways I could find and this isn't necessarily the best answer, but:

    You can do this.

    Let i = ActiveDocument.Tables.Count

    "i" then becomes the number of the last table in the document.

    In my case, I am adding tables to the end of the document, in the order that I want them to appear in the final version.

    I am always adding 1-4 tables at a time that need to be reformatted.

    Because each set of tables is predictable, I know what order they will appear.

    The answer for me was 

    Let i = ActiveDocument.Tables.Count - 1

    I could select the last few tables with -1 or -4.

    When I wanted to select a particular table in my process I would do this.

    Dim pTestTable As Table

    Set pTestTable = ActiveDocument.Tables(i)

    The i then becomes the variable to select the desired table, counting backwards from the last table in the document.

    • Marked as answer by jbhammon Tuesday, July 31, 2012 3:56 PM
    Tuesday, July 31, 2012 3:56 PM