Consulting

Results 1 to 3 of 3

Thread: Forcing cross-references to ignore bookmark names

  1. #1
    VBAX Regular YossiD's Avatar
    Joined
    Jan 2009
    Posts
    33
    Location

    Forcing cross-references to ignore bookmark names

    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?

  2. #2
    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
    Last edited by georgiboy; 08-20-2024 at 12:28 AM. Reason: Removed spam

  3. #3
    VBAX Newbie
    Joined
    Oct 2024
    Location
    https://uno-online.io
    Posts
    1
    Location
    Quote Originally Posted by augustllee View Post
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •