PDA

View Full Version : Table navigation - most reliable?



bejmark
09-19-2007, 03:21 PM
What is the most reliable way to navigate tables and act on them depending on what is found?

Thus far, I've usually used Selection.* and various searches, which works so long as there is no duplication.

It seems that using the objects directly makes more sense. However, how can I be certain which table is Table(1), when another table may later be created in that same document (or template)? Are the table numbers always in top-to-bottom sequence, or historical as the tables are created?

It seems to me that using a table name is best (in terms of reliability of operation of any given macro), but is there any way besides a special-purpose macro of setting this name in a template so that it can be used?

If using the table name, however, the selection point does not move. How then does one extract text from a given cell, or insert text into a given cell based on text that is already there?

OTWarrior
09-20-2007, 02:47 AM
Personally, I tend to use bookmarks to reference any cells, that way you can jump to a specific cell rather than having to count how many rows/columns in the table.

The good thing I found about bookmarks, is you can have multiple bookmarks for one section
ie:
(assume cells 1 and 2 are in the first row)
ref' | Bookmark name

cell1 = bmCell1
cell2 = bmCell2
Row1 = bmRow1

this way you can choose to go to a specific row or cell.
Of course, this does mean having to go in manually and enter all the bookmark names, but this would be the way I did it.

btw: code for selecting the bookmark in case you don't know it:
ActiveDocument.Bookmarks("Cell1").select

bejmark
09-20-2007, 01:51 PM
As I understand your suggestion, this would work in a document already opened, in which one would leave a bookmark (can there be more than one?) so as to get back to it.

My situation is that I am using a standardized report template, certain table cells of which need to be filled in. This filling in is fraught with human error, which is why I am trying to automate it. The report is opened anew, at which point I would like to fill in these table cells. The same data is used ultimately to name the file.

Ultimately, I would like to progress to having a button in an Excel spreadsheet (which tracks the studies for which these reports are being generated) open the correct document template and then immediately fill in the fields based on the data in the spreadsheet. If I can figure out how to do this.

--
Mark

fumei
09-20-2007, 08:30 PM
Bookmarks are probably the best way to go. You would need to give more details in order to give details back.

The Index of the Tables collection is the order in the document. So if you move the second table - ActiveDocument.Tables(2) - in a document before the first one, then IT automatically becomes ActiveDocument.Table(1).

If you inserted a table before it (while it is still #2), then it automatically becomes ActiveDocument.Tables(3).

Therefore, what I do is bookmark the tables, all of them. Tables usually contain something meaningful, so I use THAT as the name of the bookmark.

So say you have a table of client names, and phone numbers. I may name it (the bookmark) PhoneNum. NOW you can easily access THAT table, no matter where it is in the document. It can be first in the document, or the fifteenth.

Yes, its Table index will change....but the bookmark name will not. Further, you can increase/decrease the number of rows/columns in the table. It does not matter. Any change to the table will still be within the bookmark. The bookmark adjusts. The table can be moved anywhere and you can still access it directly by name.

So, say you have a table bookmarked as PhoneNum.Dim oTable As Table
Set oTable = ActiveDocument.Bookmarks("PhoneNum") _
.Range.Tables(1)

The oTable object is now THAT table. All the properties of tables are available to it. Including, of course, cells.

You can go deeper.

You can make a Cell object.Dim oTable As Table
Dim oCell As Cell
Set oTable = ActiveDocument.Bookmarks("PhoneNum") _
.Range.Tables(1)
Set oCell = oTable.Cell(2,3)
oCell.Range.Text = "hello world"
Set oCell = Nothing
Set oTable = NothingThis would make the text in Cell(2,3) of the bookmarked table PhoneNum "hello world" - regardless of where that table is in the document.

Including headers and footers. This can be very very handy. I am attaching a demo document. Click "Table Stuff" on the top toolbar. You get an inputbox. Type something and press Enter.....or if you are one of those weird people that move their hands from the keyboard to pick up the mouse and click OK....click OK.

The text you entered will appear in the header, as it is a table, and that table has been bookmarked. Further, I also made the same text appear in a different table, in the document.Sub TableStuff()
Dim oCell As Cell
Dim strIn As String
strIn = InputBox("Enter some text.", "Put in Header")
Set oCell = ActiveDocument.Bookmarks("MyHeader").Range.Tables(1).Cell(1, 2)
oCell.Range.Text = strIn
If strIn <> "" Then
ActiveDocument.Bookmarks("Tinkerbell") _
.Range.Tables(1).Cell(3, 3).Range.Text = _
"This is what you entered into the header: " _
& strIn
Else
ActiveDocument.Bookmarks("Tinkerbell") _
.Range.Tables(1).Cell(3, 3).Range.Text = _
"Nothing entered, header is blank."
End If
Set oCell = Nothing
End SubSo if you run the macro, and simply press enter, the string is blank - "" - and the header contents will be blank, and the cell in the other table will change accordingly.

Using bookmarks and table or cell objects is very powerful, and very flexible.

BTW: as a good practice, you test to see if the bookmark exists first, before trying to use it. Otherwise, say someone deleted the bookmarked table, there would be a run-time error.

fumei
09-20-2007, 08:33 PM
Oh, and doing it this way, you NEVER have to make a selection. The screen does not jump around, because there is no movement. The document simply...changes.