PDA

View Full Version : Bookmarking Tables



murphy84
08-13-2007, 05:24 AM
Does anyone here know if you can select a specific table then access rows by using a bookmark as a reference to the table?

And if so, how would I go about doing that?

I know it's possible to select a row using a range within a table but is it possible to do that with a bookmark first, then select the table then the row within the table.

Thanks in advance.

geekgirlau
08-13-2007, 08:35 PM
What do you see as being the benefit of using the bookmark?

murphy84
08-14-2007, 01:33 AM
I think I may have figured it. Here is an example portion of my code which shows what I was planning on accomplishing (in part):-

Dim bMark As bookmark
Dim start_sel_page As Integer
Dim end_sel_page As Integer

' Need to sort by range when using otherwise it wont work correctly
For Each bMark In ActiveDocument.Range.bookmarks

If InStr(bMark.Name, "tbl") Then
curr_bMark = bMark.Name ' Store the current bookmark name for later use
ActiveDocument.bookmarks(curr_bMark).Select ' Select the current bookmark
ActiveDocument.bookmarks("\StartOfSel").Select
start_sel_page = Selection.Information(wdActiveEndPageNumber)

ActiveDocument.bookmarks(curr_bMark).Select
ActiveDocument.bookmarks("\EndOfSel").Select
end_sel_page = Selection.Information(wdActiveEndPageNumber)

MsgBox "Current Selection: " & curr_bMark & " starts on page " & start_sel_page & " - ends on " & end_sel_page
' If it's not on the same page
If end_sel_page > start_sel_page Then
'Split this table here
'Select top of previous bookmark
ActiveDocument.bookmarks(curr_bMark).Select
ActiveDocument.bookmarks("\StartOfSel").Select
Selection.InsertBreak Type:=wdSectionBreakNextPage
MsgBox curr_bMark & " spans onto the next page (" & start_sel_page & " and " & end_sel_page
Else
' Do nothing
MsgBox curr_bMark & " is only on page " & start_sel_page
End If
Else
End If
Next

The above code splits portions of the table so that they lie entirely on one page instead of overlapping. You have to put bookmarks in manually first however.

fionabolt
08-15-2007, 01:17 AM
Mark,
as GeekgirlAU asks


What do you see as being the benefit of using the bookmark?


If you are trying to format all the tables in a document, why not do something like:
Sub reAlignTables()
Dim tbl As Table
Dim rTbl As Range
Dim iPageStart As Integer
Dim iPageEnd As Integer
Dim tblCount As Long
If ActiveDocument.Tables.Count >= 1 Then
For Each tbl In ActiveDocument.Tables
tblCount = tblCount + 1
Set rTbl = tbl.Range
rTbl.SetRange tbl.Range.Start, tbl.Range.Start
iPageStart = rTbl.Information(wdActiveEndPageNumber)
rTbl.SetRange tbl.Range.End, tbl.Range.End
iPageEnd = rTbl.Information(wdActiveEndPageNumber)
MsgBox "Table " & tblCount & " starts on page " & iPageStart & " - ends on " & iPageEnd
' If it's not on the same page
If iPageEnd > iPageStart Then
'Split this table here
tbl.AllowPageBreaks = False
MsgBox tblCount & " spans onto the next page (" & iPageStart & " and " & iPageEnd & ")"
Else
' Do nothing
MsgBox "Table " & tblCount & " is only on page " & iPageStart
End If
Next tbl
End If
End Sub


If I have understood your requirements correctly, there is no need to assign any bookmarks. Just loop through all the Tables in the document. Also, no need to insert a page break, just apply table formatting so that AllowPageBreaks = False.

F

fumei
08-22-2007, 09:00 AM
Actually, I usually DO bookmark all my tables. Why?

Because then I can access them easily by name. The Tables collection uses an index. By bookmarking the tables, I can do things like:Dim aTable As Table
Set aTable = ActiveDocument.Bookmarks("History").Range.Tables(1)
Msgbox aTable.Cell(2,3)and get the contents of Cell(2,3) from a specific table by name.

Not only that, but by bookmarking each table you can shuffle the tables around as much as you like. You can move them around and still access them easily because you are no longer accessing them by index number. When you move a table in a document, its index number changes. But if it is bookmarked, you access it by name. The index number of no longer relevant. The table may be accessed independently of its index number.

You can change the number of rows or columns...whatever...the bookmark range adjusts automatically.

Here is an example of a use.
Sub GetEachSpecificTable()
Dim aTable As Table
Dim aFile
Dim strWhatever As String
Const myPATH As String = "C:\Test\"

aFile = Dir(myPATH & "*.doc")
Do While aFile <> ""
Documents.Open FileName:=myPATH & aFile
If ActiveDocument.Bookmarks.Exists("History") = True Then
Set aTable = ActiveDocument.Bookmarks("History") _
.Range.Tables(1)
strWhatever = strWhatever & aTable.Cell(2, 3) _
.Range.Text & vbCrLf
ActiveDocument.Close wdDoNotSaveChanges
aFile = Dir
Else
ActiveDocument.Close wdDoNotSaveChanges
aFile = Dir
End If
Loop
MsgBox strWhatever
End SubThis gets the text from cell(2,3) from a table bookmarked as "History" from each file in the test folder - if it exists. If the document does not have a History table, the code closes the file and goes to the next one.

The History table can be the third one - .Tables(3) - or the fifth one - .Tables(5) - or the 17th one - .Tables(17). It does not matter, because it is accessed by name.

Sure, you could run through all the tables in the document and find the correct one. And, of course, if it is ALWAYS a specific index number (eg. ActiveDocument.Tables(4)) this is not a problem. But by bookmarking it, making a Table object of it is straightforward, regardless of where it is.

Including Headers.

While you can not GoTo (Insert > Bookmarks) a bookmark in a header, you CAN set a Table object for a bookmarked table in a header.Dim aTable As Table
Set aTable = ActiveDocument.Bookmarks("SecondPageHeaderTable") _
.Range.Tables(1)
aTable.Cell(1, 2).Range.Text = "Yum Yum"This is because bookmarks in headers are part of the Bookmarks collection, and thus exposed through that collection.