PDA

View Full Version : Searching sections



tla
10-07-2008, 09:50 AM
I have a document with several sections. In one section is a particular table that I reference with some Macros. Becuase others routinely edit the base document and could possible add tables above or below the table in question, I am running into some trouble. What I want to do is isolate this table in it's own section, and then search only that section for the correct table. The Tables collection ignores sections so I am not sure how to identify the first table in a given section.

Any Ideas?

macropod
10-09-2008, 04:35 AM
Hi tla,

If your users can add tables to the document, there's noting to stop them from adding more tables to the section your table is in.

I suggest bookmarking the table, then selecting the table in the bookmarked range. For example, suppose the table's bookmark is named 'myTbl'. You could use something along the lines of:
Sub UpdateTable()
With ActiveDocument.Bookmarks("myTbl").Range.Tables(1)
.Cell(1, 2).Range.Text = "Hello"
.Cell(2, 2).Range.Text = "Goodbye"
End With

End Subto insert "Hello" into B1 and "Goodbye" into B2.

fumei
10-10-2008, 02:40 PM
To extend that thought, you can make a table object of that table - after you bookmark it as macropod suggests. Now it does not matter if you put tables before it, or move the table itself, or whatever. Assuming the same name as macropod...Dim oTable As Table
Set oTable = ActiveDocument.Bookmarks("myTbl").Range.Tables(1)

With oTable
.Cell(1, 2).Range.Text = "Hello"
.Cell(2, 2).Range.Text = "Goodbye"
End With

The advantage of this is that you can access all the properties and methods of THAT table. If you declare oTable as a global (Public) variable, and Set it, you can use it in as many Sub routines as you like. It will no difference what Section it is in.