MS Office Forum / Word / Programming / November 2006
How do I access the content of the InsertCrossReference command?
|
|
Thread rating:  |
Goody - 08 Nov 2006 16:32 GMT I am using the code shown below to populate a table. I assign each page a bookmark, return to the table, insert a cross reference to the bookmark, delete the bookmark, go to the next page, and repeat the process. The procedure keeps track of the row and column positions, and moves the selection point back to the top of the table and two columns to the right when the last row is reached. For some reason, when the selection is in anything other than the first column, the InsertCrossReference command still puts its reference in the first column. I am trying to work around this "feature", but I need a way to address the contents of the InsertCrossReference method. How do I accomplish this?
Alternatively, if anyone can explain why the cross reference is always placed in the first column, I might be able to avoid the work around.
Thanks, Goody *************************** Sub Macro1() Dim tblRevList As Table Dim 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:="" 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:="" tblRevList.Cell(irow, icolumn).Range.InsertCrossReference _ ReferenceType:=wdRefTypeBookmark, ReferenceKind:=wdPageNumber, _ ReferenceItem:="here", InsertAsHyperlink:=False, IncludePosition:=False, _ SeparateNumbers:=False, SeparatorString:=" " 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 End Sub
Jean-Guy Marcil - 08 Nov 2006 19:13 GMT Goody was telling us: Goody nous racontait que :
> I am using the code shown below to populate a table. I assign each > page a bookmark, return to the table, insert a cross reference to the [quoted text clipped - 10 lines] > Alternatively, if anyone can explain why the cross reference is always > placed in the first column, I might be able to avoid the work around. I have some difficulty understanding your code because I do not understand its purpose.
Why are you setting a cross-reference to a bookmark and then immediately delete that bookmark? The cross-reference will become useless, no?
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
Goody - 08 Nov 2006 19:53 GMT I am building a technical report that features a page that lists each page number and the applicable revision level for that page. The use of the bookmark and cross reference is the only method I could think of that gives me the formatted page number, which includes the chapter number. The code I included omitted a few lines. One of the missing lines is:
tblRevList.Range.Fields.Unlink ' Unlink cross-reference fields in table.
The cross references do not get updated during the macro execution, so deleting the bookmark does not affect the output. Hope that clears it up.
Thanks, Goody
> Goody was telling us: > Goody nous racontait que : [quoted text clipped - 19 lines] > Why are you setting a cross-reference to a bookmark and then immediately > delete that bookmark? The cross-reference will become useless, no? Jean-Guy Marcil - 09 Nov 2006 02:54 GMT Goody was telling us: Goody nous racontait que :
> I am building a technical report that features a page that lists each > page number and the applicable revision level for that page. The use [quoted text clipped - 9 lines] > deleting the bookmark does not affect the output. Hope that clears it > up. Ha, now it makes sense!
It seems that this is a bug. I have tried to insert it using all kinds of ways, now cigar!
Here's a workaround I have cleaned up your code to make it shorter, faster and more reliable, (note that since you decided to use the Page object, this is only going to work with Word 2003 and up).
Sub Macro1()
Dim tblRevList As Table Dim irowcount As Integer Dim icolcount As Integer Dim irow As Long Dim icolumn As Long Dim iPage As Page Dim rgeCell As Range
Set tblRevList = ActiveDocument.Tables(2)
With tblRevList icolumn = 1 irow = 2 irowcount = .Rows.Count - 1 icolcount = .Columns.Count
For Each iPage In ActiveDocument.ActiveWindow.ActivePane.Pages ' Set bookmark for reference. ActiveDocument.Bookmarks.Add "here", iPage.Rectangles(1).Range 'Set range to transfer from first col to other col Set rgeCell = .Cell(irow, icolumn).Range .Cell(irow, icolumn).Range.InsertCrossReference _ wdRefTypeBookmark, wdPageNumber, "here", False, False, _ False, " " ' There seems to be a bug with the InsertCrossReference method _ in tables When inserting in column above 1, Word insists to _ insert in first column 'Tansfer from first to target column If icolumn > 1 Then rgeCell.FormattedText = .Cell(irow, 1).Range.Fields(1).Result .Cell(irow, 1).Range.Fields(1).Delete End If ' Is cell at the bottom of the table? If irow = irowcount + 1 Then ' If so, move two columns to the right ' and back to the top of the table. icolumn = icolumn + 2 irow = 2 Else ' Otherwise, move to next row irow = irow + 1 End If Next
' Unlink cross-reference fields in table. .Range.Fields.Unlink ' Delete bookmark ActiveDocument.Bookmarks("here").Delete End With
End Sub
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
Jean-Guy Marcil - 10 Nov 2006 12:06 GMT Jean-Guy Marcil was telling us: Jean-Guy Marcil nous racontait que :
> Goody was telling us: > Goody nous racontait que : [quoted text clipped - 21 lines] > faster and more reliable, (note that since you decided to use the > Page object, this is only going to work with Word 2003 and up). Thanks to Cindy M. case solved! She posited that because the cell marker (¤) was part of the range where the Cross-Reference was being inserted, it affected the outcome.
The following code excludes the cell marker and now everything behaves as it should.
Sub Macro1()
Dim tblRevList As Table Dim irowcount As Integer Dim icolcount As Integer Dim irow As Long Dim icolumn As Long Dim iPage As Page Dim rgeCell As Range
Set tblRevList = ActiveDocument.Tables(2)
With tblRevList icolumn = 1 irow = 2 irowcount = .Rows.Count - 1 icolcount = .Columns.Count
For Each iPage In ActiveDocument.ActiveWindow.ActivePane.Pages ActiveDocument.Bookmarks.Add "here", iPage.Rectangles(1).Range Set rgeCell = .Cell(irow, icolumn).Range rgeCell.Collapse wdCollapseStart rgeCell.InsertCrossReference _ wdRefTypeBookmark, wdPageNumber, "here", False, False, _ False, " " ' Is cell at the bottom of the table? If irow = irowcount + 1 Then ' If so, move two columns to the right ' and back to the top of the table. icolumn = icolumn + 2 irow = 2 Else irow = irow + 1 End If Next
.Range.Fields.Unlink ActiveDocument.Bookmarks("here").Delete End With
End Sub
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
Goody - 13 Nov 2006 16:34 GMT You are correct in assuming that the case is solved for a simple page number. However, my document has page numbering in the Chapter-Page format. When I run your solution in such a document, every cell in the table is populated with the number 1. The copy/paste/delete procedure yields the correct result, so I'm using that. I am still interested in a simpler solution if possible.
Thanks for your help, Goody
> Jean-Guy Marcil was telling us: > Jean-Guy Marcil nous racontait que : [quoted text clipped - 73 lines] > > End Sub Jean-Guy Marcil - 13 Nov 2006 18:57 GMT Goody was telling us: Goody nous racontait que :
> You are correct in assuming that the case is solved for a simple page > number. However, my document has page numbering in the Chapter-Page > format. When I run your solution in such a document, every cell in > the table is populated with the number 1. The copy/paste/delete > procedure yields the correct result, so I'm using that. I am still > interested in a simpler solution if possible. It seems that there is a bug with the InsertCrossReference method when used in conjunction with chapter umbers.
If I use the recorder, I get:
Selection.InsertCrossReference ReferenceType:="Bookmark", ReferenceKind:= _ wdPageNumber, ReferenceItem:="here", InsertAsHyperlink:=False, _ IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
which is the same as I posted before.
But, if I run that recorded code by doing ALT-F8, I also get noting but a "1"...
So I guess you are going to have to stick to your hammer...
But, if you are interested in reading what some of my MVP colleagues who are expert in authoring long documents, especially technical ones, had to say about this page revision technique project you are involved with, let me know, I'll post their comments, nut I must warn you that you may not like it!
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
Goody - 13 Nov 2006 19:03 GMT Please post their comments. I need a dose of humility!
Thanks, Goody
> Goody was telling us: > Goody nous racontait que : [quoted text clipped - 28 lines] > know, I'll post their comments, nut I must warn you that you may not like > it! Jean-Guy Marcil - 13 Nov 2006 21:59 GMT Goody was telling us: Goody nous racontait que :
> Please post their comments. I need a dose of humility! You asked for it!
Now, looking at your overall goals, here are some comments from two very knowledgeable people who have dealt with technical documents for a long time and for large corporation:
1) <quote> That is where he is going wrong. He is trying to automate in Word a 1960's procedure for document revision control, where users receive a set of updated retyped pages and solemnly go through their ring binders replacing pages.
Or more accurately, they dump the revised pages loose in the back of the folder and promise themselves that they will get round to inserting them sometime.
If he is updating a whole document at a time in Word, the whole document should be sharing the same revision level, at which point the entire problem disappears.
I've just been through this loop with a client - a regional airline which I will not shame by naming them. They have had a page-based update process for their operating manuals, including the pilots manuals. I provided templates to automate the process, but before doing so strongly advised that they should change away from a page-based update process.
I was ignored, and provided the templates anyway. They have ever since been trying to get it to work, finding that they don't have the skilled personnel to make it work, and have finally admitted that the pilot manuals and crew manuals are probably out of date because they aren't doing the page replacement properly.
After a year of struggling with this, I had a meeting with them yesterday, and they have finally taken my advice and agreed to move at least to updating whole chapters for the longer manuals and updating whole documents for the shorter ones, as a stepping-stone on the way to electronic distribution of these documents, and probably towards equipping the cockpits of all their planes with tablet PCs on which the manuals can be displayed. (This will replace the 40kg of paper manuals every plane carries around with it on every flight.) <unquote>
2) <quote> I would have added an XE index tag to the revision level on each page.
The index generator would then return the list sorted by revision level with the formatted page number of each.
But I would have given the original poster a lengthy sermon on "not doing this" followed by a reference to the Esso Gas Disaster. Which is just one disatser produced by attempting to maintain technical manuals by page replacement and try to ensure that each page has the latest applicable revision level
http://ourcivilisation.com/decline/gasbang.htm <unquote>
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
|
|
|