D
dedawson
I have some code that marks the current selection point, finds the
next caption, and inserts a cross-reference to that caption at the
marked selection point. The problem is that it will sometimes fail as
a result of a failure of the GetCrossReferenceItems method to return
the correct number of items.
Here's my lead-in code
ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="PutItHere"
Selection.Find.Style = ActiveDocument.Styles("Caption")
Selection.Find.Execute
cFieldResults = Selection.Fields(1).Result + "-" +
Selection.Fields(2).Result
oCaptions = ActiveDocument.GetCrossReferenceItems(wdCaptionTable)
And here's some code right out of Microsoft Visual Basic Help.
oCaptions = ActiveDocument.GetCrossReferenceItems(wdCaptionTable)
For I = 1 To UBound(oCaptions)
If InStr(LCase$(oCaptions(I)), cFieldResults) Then
iReferenceItem = I
Exit For
End If
Next I
and here's the rest of my code
Selection.GoTo What:=wdGoToBookmark, Name:="PutItHere"
Selection.InsertCrossReference ReferenceType:="Table",
ReferenceKind:= _
wdOnlyLabelAndNumber, ReferenceItem:=iReferenceItem, _
InsertAsHyperlink:=False, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:="
"
My document contains 102 entries in wdCaptionTable. If I single step
through this code using F8, it is succesful every time --
UBound(oCaptions) always = 102
If, however, I merely start the code by F5, sometimes the code works,
and other times it fails. When it fails, it is because iReferenceItem
is Empty, which is the result of UBound(oCaptions) having been set to
6 or 7. In this case, the loop never finds the Caption that was found
by Selection.Find.Execute because it terminates before it reaches the
index of the Caption.
I am at a complete loss, and hope someone can clue me in on
a) why this is happening, and
b) how can it be corrected?
Many thanks in advance
david
next caption, and inserts a cross-reference to that caption at the
marked selection point. The problem is that it will sometimes fail as
a result of a failure of the GetCrossReferenceItems method to return
the correct number of items.
Here's my lead-in code
ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="PutItHere"
Selection.Find.Style = ActiveDocument.Styles("Caption")
Selection.Find.Execute
cFieldResults = Selection.Fields(1).Result + "-" +
Selection.Fields(2).Result
oCaptions = ActiveDocument.GetCrossReferenceItems(wdCaptionTable)
And here's some code right out of Microsoft Visual Basic Help.
oCaptions = ActiveDocument.GetCrossReferenceItems(wdCaptionTable)
For I = 1 To UBound(oCaptions)
If InStr(LCase$(oCaptions(I)), cFieldResults) Then
iReferenceItem = I
Exit For
End If
Next I
and here's the rest of my code
Selection.GoTo What:=wdGoToBookmark, Name:="PutItHere"
Selection.InsertCrossReference ReferenceType:="Table",
ReferenceKind:= _
wdOnlyLabelAndNumber, ReferenceItem:=iReferenceItem, _
InsertAsHyperlink:=False, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:="
"
My document contains 102 entries in wdCaptionTable. If I single step
through this code using F8, it is succesful every time --
UBound(oCaptions) always = 102
If, however, I merely start the code by F5, sometimes the code works,
and other times it fails. When it fails, it is because iReferenceItem
is Empty, which is the result of UBound(oCaptions) having been set to
6 or 7. In this case, the loop never finds the Caption that was found
by Selection.Find.Execute because it terminates before it reaches the
index of the Caption.
I am at a complete loss, and hope someone can clue me in on
a) why this is happening, and
b) how can it be corrected?
Many thanks in advance
david