how to identify a given table

E

Eric

What is a guaranteed way to identify a given table. Index is not reliable in
the document template I need to work with, as the table of "scheduled
values" can be either table[5] or table[6], depending on whether a given
document will contain "specification documents" and/or "illustrations".

Thanks In Advance,
Eric
 
H

Helmut Weber

Hi Eric,

bookmark the table.
alt num 5, insert bookmark.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
E

Eric

I'm missing something - how do I refer to this as a table? Bookmark the
entire table? Is this returning a range I can turn into a table?

dim tbl as word.table
set tbl = myDoc.Bookmarks("myTable")
 
H

Helmut Weber

Hi Eric

for testing in single step mode [F8]

Sub Test0001()
Dim oTbl As Table
Dim rTmp As Range
ActiveDocument.Bookmarks("Table2").Select
Set rTmp = ActiveDocument.Bookmarks("Table1").Range
rTmp.Select
Set oTbl = ActiveDocument.Bookmarks("Table3").Range.Tables(1)
oTbl.Select
End Sub





--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jay Freedman

You can bookmark the whole table, or just ensure that the bookmark occurs
somewhere inside the table. Then the following code will work:

Dim tbl As Word.Table
Set tbl = myDoc.Bookmarks("myTable").Range.Tables(1)

It's a general principle of Word VBA that the first table (or paragraph, word,
etc.) "in" a range is actually the first one of which any part is in the range.

If you're using this kind of code, though, you should first ensure that there is
such a bookmark and that it does contain (part of) a table:

Dim tbl As Word.Table
If ActiveDocument.Bookmarks.Exists("myTable") Then
If myDoc.Bookmarks("myTable").Range.Tables.Count > 0 Then
Set tbl = myDoc.Bookmarks("myTable").Range.Tables(1)
End If
End If

If Not tbl Is Nothing Then
' continue the macro...
 
E

Eric

Hi Jay

Love your code, as usual, and it explains what I didn't know perfectly. See if you can make sense of the following. I extracted a totally trivial routine as I'm a fan of DRY (Don't Repeat Yourself!). The routine is:

Public Sub DeleteAllRowsButHeader(tbl As Word.Table)
Dim k As Integer
With tbl
'delete all rows first
For k = .Rows.Count To 2 Step -1
.Rows(k).Delete
Next k
End With
End Sub


One table routine calls this without a problem:
Public Sub PopulateScheduleOfValues(tbl As Word.Table)
MWordTable.DeleteAllRowsButHeader tbl
MTransferData.CreatePayAppDataToTransfer
PopulateSovTable tbl, MTransferData.sovLineItems
End Sub


While another generates a type mismatch when it calls it:
Public Sub PopulateVariablesChanged(tbl As Word.Table)
MWordTable.DeleteAllRowsButHeader (tbl) <------------NOW the Delete routine wants a Range as a parameter
PopulateVariablesChangedTable tbl, tbl.Parent.FormFields
End Sub

Stuff like this makes me want to run as from VBA as possible! Anyway, thanks again for all of your help.

- Eric
 
J

Jay Freedman

Hi Eric,

You're right, that makes no sense at all.

The rule is supposed to be that the call requires parentheses if it returns a
value (i.e., it's a call to a function) and requires no parentheses if it
doesn't return a value. I always thought it was sloppy thinking on the part of
the VB designers that there was any distinction at all, and really stupid that
parentheses wouldn't be accepted when they weren't necessary. I suspect it was
done to make things easier for the guy who wrote that part of the
interpreter/compiler.

But to have it go one way in one module and the other way in another module is
unbelievable -- I've never seen that happen.
 

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