The table that I am building is a revision list, with each page number listed
in a cell, and the revision letter of that page in the adjacent cell. Since
my first posting, I have developed a procedure that accomplishes what I was
trying to do, by placing a bookmark at the start of a page, then placing a
cross reference to the bookmark in the appropriate table cell. It gives me
the proper page number format, but it is not working as intended. When it
reaches the bottom of the table, it is supposed to return to the first blank
cell, which is row #2, two columns to the right. For some reason, the
InsertCrossReference method overwrites the data in the first column. The
InsertAfter command on the next line puts its argument in the proper cell. I
cannot determine why this is occurring. The code is shown below.
I would appreciate any suggestions you might have.
Goody
**************************
Sub PopulateRevList()
'
' Macro written 11/03/2006 by ray.dale
' It lists all of the pages in a document in the revision list table, along
with "-" for each page.
'
Dim tblRevList As Table
Dim columnstart As Integer, rowstart As Integer, irowcount As Integer,
icolcount As Integer
Dim irow As Long, icolumn As Long
Set tblRevList = ActiveDocument.Tables(2)
icolumn = 1
irow = 2
irowcount = tblRevList.Rows.Count - 1
icolcount = tblRevList.Columns.Count
Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=1, Name:=""
' Move cursor to start of document.
' Move through the document page by page
For Each ipage In ActiveDocument.ActiveWindow.ActivePane.Pages
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="here"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With ' Set bookmark for reference.
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=2,
Name:=""
' Move to Page List table.
MsgBox "The row number is " & irow & ", and the column number is " &
icolumn
tblRevList.Cell(irow, icolumn).Range.InsertCrossReference
ReferenceType:=wdRefTypeBookmark, ReferenceKind:= _
wdPageNumber, ReferenceItem:="here", InsertAsHyperlink:=False, _
IncludePosition:=False, SeparateNumbers:=False,
SeparatorString:=" "
' Move to next page number cell and insert current page number of bookmark.
tblRevList.Cell(irow, icolumn + 1).Range.InsertAfter "-" ' Enter
hyphen in adjacent cell.
Selection.GoTo What:=wdGoToBookmark, Name:="here" ' Return to
bookmark location.
ActiveDocument.Bookmarks("here").Delete ' Clear bookmark
Application.Browser.Target = wdBrowsePage ' Change to browse by page
Application.Browser.Next ' Move to top of next page
If irow = irowcount + 1 Then ' Is cell at the bottom of the table?
icolumn = icolumn + 2 ' If so, move two columns to the right
irow = 2 ' and back to the top of the table.
Else
irow = irow + 1 ' Otherwise, move to next row
End If
Next
tblRevList.Range.Fields.Unlink ' Unlink cross-reference fields in table.
Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=2, Name:=""
' Return to start of Revision List table
End Sub
**********************