Log in

View Full Version : Forcing cross-references to ignore bookmark names



YossiD
02-04-2024, 05:56 AM
I have these commands in a Word macro (Word 2013):


Selection.InsertCrossReference ReferenceType:="Heading", _
ReferenceKind:=wdNumberNoContext, ReferenceItem:=Step, _
Selection.TypeText Text:=vbTab
Selection.InsertCrossReference ReferenceType:="Heading", _
ReferenceKind:=wdContentText, ReferenceItem:=Step, InsertAsHyperlink:=True _
, IncludePosition:=False InsertAsHyperlink:=True, IncludePosition:=False

For each Heading in a document, these commands print the heading number followed by a tab followed by the heading text. Like this:
‎1 This is a first level heading with bookmark
‎2 This a first level heading without bookmark

Some of the headings are bookmarked, and in those cases the cross-references use the bookmark names rather than generic references. Like this:

{ REF FirstLevelHeadingwBM \n \h } { REF FirstLevelHeadingwBM \h }

For headings that are not bookmarked, the cross-references look like this:

{ REF_Ref157949988 \n \h } { REF _Ref157949988 \h }

Since I might delete some of the bookmarks at a later time, I want the macro to only use the generic Refs and to never use the bookmark names. How can I modify the code to prevent using the bookmark names as Refs in the cross-references?

Also, is there a simple way to get the \* charformat switch incorporated in the cross-references, or would I need to add them using separate statements?

augustllee
08-13-2024, 06:46 PM
To ensure that only generic references are used, we can leverage the fact that bookmarks have a specific structure. By checking if the heading's name starts with "_Ref", we can determine if it's a generic reference.


VB.Net
Sub InsertCrossReferences()
Dim Heading As Heading

For Each Heading In ActiveDocument.Headings
Selection.InsertCrossReference ReferenceType:="Heading", _
ReferenceKind:=wdNumberNoContext, ReferenceItem:=Heading.Name, _
InsertAsHyperlink:=True, IncludePosition:=False
Selection.TypeText Text:=vbTab

' Check if the heading name is a generic reference
If Left(Heading.Name, 4) <> "_Ref" Then
' If not, use generic reference
Selection.InsertCrossReference ReferenceType:="Heading", _
ReferenceKind:=wdContentText, ReferenceItem:=Heading.Name, _
InsertAsHyperlink:=True, IncludePosition:=False
Else
' If it's a generic reference, use the existing cross-reference
Selection.InsertCrossReference ReferenceType:="Heading", _
ReferenceKind:=wdContentText, ReferenceItem:=Heading.Name, _
InsertAsHyperlink:=True, IncludePosition:=False
End If
Next Heading
End Sub

Baylee Marqu
10-01-2024, 03:18 AM
To ensure that only generic references are used, we can leverage the fact that bookmarks have a specific structure. By checking if the heading's name starts with "_Ref", we can determine if it's a generic reference.


VB.Net
Sub InsertCrossReferences()
Dim Heading As Heading

For Each Heading In ActiveDocument.Headings
Selection.InsertCrossReference ReferenceType:="Heading", _
ReferenceKind:=wdNumberNoContext, ReferenceItem:=Heading.Name, _
InsertAsHyperlink:=True, IncludePosition:=False
Selection.TypeText Text:=vbTab

' Check if the heading name is a generic reference
If Left(Heading.Name, 4) <> "_Ref" Then
' If not, use generic reference
Selection.InsertCrossReference ReferenceType:="Heading", _
ReferenceKind:=wdContentText, ReferenceItem:=Heading.Name, _
InsertAsHyperlink:=True, IncludePosition:=False
Else
' If it's a generic reference, use the existing cross-reference
Selection.InsertCrossReference ReferenceType:="Heading", _
ReferenceKind:=wdContentText, ReferenceItem:=Heading.Name, _
InsertAsHyperlink:=True, IncludePosition:=False
End If
Next Heading
End Sub



Oh thank you for your reply, I have fixed it thank you very much.