Adding a row to a protected table - (attn: G. Mayor)

B

Bigfoot17

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

Fumei2 via OfficeKB.com

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
 
B

Bigfoot17

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

Fumei2 via OfficeKB.com

Ah, yes. Merged cells. The bane of tables and VBA. I avoid them like the
plague.

Glad I could help. Yes, the table index number is the order the table
CURENTLY is in the document. So there is no connection, really, between the
real table , and its index number. The index number is only the order the
table is in the document; it is not - really - the table. The index numbe
rpoints to the first (or second, or third...) table, not to a specific table
AS A TABLE.

By using table objects you can have a pointer to s specific table, REGARDLESS
of where it is in the document. I find this very very handy. Here is a real
world example.

I have a template that is used a lot. It has two tables, each is bookmarked
(ClientNames & ClientAddress).

I have used the template to make many many documents...say 100. So I now
have 100 document, each with two tables (identified as ClientNames &
ClientAddress)


Sub getData()
Dim oTable1 As Table
Dim oTable2 As Table
Dim file
Dim path As String
Dim ThisDoc As Document

Set ThisDoc = ActiveDocument
path = "c:\yadda\ClientCrap\" ' not real path!

file = Dir(path & "*.doc")
Do While file <> ""
Documents.Open Filename:=path & file
Set oTable1 = ActiveDocument.Bookmarks("ClientName").Tables(1)
Set oTable2 = ActiveDocument.Bookmarks("ClientAddress").Tables(1)
ThisDoc.Range.InsertAfter Text:= oTable1.Cell(2,1).Range.Text & _
oTable2.Cell(1,1).Range.Text & vbCrLf
ActiveDocument.Close wdDoNotSaveChanges
file = Dir()
Loop
End Sub

This processes ALL Word documents in the folder "c:\yadda\ClientCrap\" - not
the real path! - extracting the text from Cell(2,1) of ClientName, and the
text from Cell(1,1) of ClientAddress and putting that text in the current
document (set as ThisDoc).

Getting that data out of 100 documents takes about 2 minutes.

This just scratches the surface of what you can do with table objects and
consistent design.
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.
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.
[quoted text clipped - 68 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top