From: Bigfoot17 on 7 Apr 2010 15:19 Graham Mayor provides a macro that is extremely helpful to me in a project I am working on entitled: "An alternative method of adding a row to a protected table " (http://www.gmayor.com/word_vba_examples.htm). What I am having trouble with is identifying the number of the table. I have a lengthy document and over time some tables have been added or deleted, and I am suspecting that I my table numbers are 'out-there.' I have a commandbutton running the above macro and if I tested the macro to add rows to tables 1-4 and it works fine. However, I cannot seem to get it to work on tables 5-8 (which may be table 52 for all I know). Set oTable = .Tables(1) 'Select the appropriate table Is there something I am missing. How can I find out the number of a table in my document?
From: Fumei2 via OfficeKB.com on 7 Apr 2010 15:50 Not really. The table index is the CURRENT order of tables in the document. If a table is inserted above, then it gets the previous index. Table1 (call it Yadda) Table2 (call it Blah) Table3 (call it Whatever) If a new table is inserted above Yadda, it now becomes Table1. Table1 Table2 (call it Yadda) Table3 (call it Blah) Table4 (call it Whatever) OR.... if a table is moved, then it gets the index number in the order it is in the document. Table1 (call it Yadda) Table2 (call it Blah) Table3 (call it Whatever) with Yadda moved to between Blah and Whatever - the index number is changed. Table1 (call it Blah) Table2 (call it Yadda) Table3 (call it Whatever) What to do? Here is what I do. I make each table bookmarked, and of course bookmarks have NAMES. If the bookmark is the table, and only one table, then you can make a table object (named of course) for that table. Thus, you can name a table, and because it comes from a bookmark, it does not matter if it is moved, or if other tables are inserted before it. Nor does it matter if add or subtract rows from the table. Any such changes remain within the bookmark range, and are automatically covered. Dim tblYadda As Table Dim tblBlah As Table Dim tblWhatever As Table Set tblYadda = ActiveDocument.Bookmarks("Yadda").Range.Tables(1) Set tblBlah = ActiveDocument.Bookmarks("Blah").Range.Tables(1) Set tblWhatever = ActiveDocument.Bookmarks("Whatever").Range.Tables(1) As these are now table OBJECTS, all properties are available. tblYadda.Cell(2,3).Range.Text = "some text" "add rows to tables 1-4 and it works fine. However, I cannot seem to get it to work on tables 5-8 (which may be table 52 for all I know). Exactly. It could be Table52, or Table6, or Table23. If you have a lot of tables, and you need to work with them a lot, bookmark them, and then use table objects. You will never have to concern yourself with WHERE they are, or how may rows they have...ever again. Gerry Bigfoot17 wrote: >Graham Mayor provides a macro that is extremely helpful to me in a project I >am working on entitled: "An alternative method of adding a row to a protected >table >" (http://www.gmayor.com/word_vba_examples.htm). What I am having trouble >with is identifying the number of the table. I have a lengthy document and >over time some tables have been added or deleted, and I am suspecting that I >my table numbers are 'out-there.' I have a commandbutton running the above >macro and if I tested the macro to add rows to tables 1-4 and it works fine. >However, I cannot seem to get it to work on tables 5-8 (which may be table 52 >for all I know). > >Set oTable = .Tables(1) 'Select the appropriate table > >Is there something I am missing. How can I find out the number of a table >in my document? -- Gerry Message posted via OfficeKB.com http://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1
From: Bigfoot17 on 9 Apr 2010 14:58 I cannot begin to tell you haw helpful this response has been. You did more than just answer the question you instructed, very helpful. As a result I could explore more and learned more about my situation and why I was having problems. First, I found I could indeed, identify the table numbers because they are always in order. Second, I learned why the macro that was running sucessfully on tables 1-4 adding an additional row, with formfields, and would not run on tables 5-8 was happening for a good reason. Tables 5-8 had merged cells in the last row (vertically). For instance Column 3, rows 2 and 3 were merged so the macro would not work. Thanks. I'm learning. "Fumei2 via OfficeKB.com" wrote: > Not really. The table index is the CURRENT order of tables in the document. > If a table is inserted above, then it gets the previous index. > > Table1 (call it Yadda) > Table2 (call it Blah) > Table3 (call it Whatever) > > If a new table is inserted above Yadda, it now becomes Table1. > > Table1 > Table2 (call it Yadda) > Table3 (call it Blah) > Table4 (call it Whatever) > > OR.... if a table is moved, then it gets the index number in the order it is > in the document. > > Table1 (call it Yadda) > Table2 (call it Blah) > Table3 (call it Whatever) > > with Yadda moved to between Blah and Whatever - the index number is changed. > > Table1 (call it Blah) > Table2 (call it Yadda) > Table3 (call it Whatever) > > What to do? Here is what I do. I make each table bookmarked, and of course > bookmarks have NAMES. If the bookmark is the table, and only one table, then > you can make a table object (named of course) for that table. Thus, you can > name a table, and because it comes from a bookmark, it does not matter if it > is moved, or if other tables are inserted before it. Nor does it matter if > add or subtract rows from the table. Any such changes remain within the > bookmark range, and are automatically covered. > > Dim tblYadda As Table > Dim tblBlah As Table > Dim tblWhatever As Table > > Set tblYadda = ActiveDocument.Bookmarks("Yadda").Range.Tables(1) > Set tblBlah = ActiveDocument.Bookmarks("Blah").Range.Tables(1) > Set tblWhatever = ActiveDocument.Bookmarks("Whatever").Range.Tables(1) > > As these are now table OBJECTS, all properties are available. > > tblYadda.Cell(2,3).Range.Text = "some text" > > "add rows to tables 1-4 and it works fine. However, I cannot seem to get it > to work on tables 5-8 (which may be table 52 for all I know). > > Exactly. It could be Table52, or Table6, or Table23. If you have a lot of > tables, and you need to work with them a lot, bookmark them, and then use > table objects. You will never have to concern yourself with WHERE they are, > or how may rows they have...ever again. > > Gerry > > Bigfoot17 wrote: > >Graham Mayor provides a macro that is extremely helpful to me in a project I > >am working on entitled: "An alternative method of adding a row to a protected > >table > >" (http://www.gmayor.com/word_vba_examples.htm). What I am having trouble > >with is identifying the number of the table. I have a lengthy document and > >over time some tables have been added or deleted, and I am suspecting that I > >my table numbers are 'out-there.' I have a commandbutton running the above > >macro and if I tested the macro to add rows to tables 1-4 and it works fine. > >However, I cannot seem to get it to work on tables 5-8 (which may be table 52 > >for all I know). > > > >Set oTable = .Tables(1) 'Select the appropriate table > > > >Is there something I am missing. How can I find out the number of a table > >in my document? > > -- > Gerry > > Message posted via OfficeKB.com > http://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1 > > . >
|
Pages: 1 Prev: Can't delete file?!!? Next: Running a macro after document as finished rendering? |