• Word 2003 VBA tool to find the targets of cross-references and insert them

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Word 2003 VBA tool to find the targets of cross-references and insert them

    Author
    Topic
    #460943

    Hi all,

    Finding the targets of cross-references and inserting them takes a relatively long time.
    I have to do it a lot.

    Anybody know of a VBA routine to find the target for a cross-reference you want to insert according to a text you highlight, and then insert it.

    Enhancements would be:
    1. If there are more than one hits then it displays the full list, one of which you click to insert.
    2. It applies the hyperlink character style of your choice, e.g., blue and underlined.

    Dreaming?

    TIA

    – avi

    Viewing 1 reply thread
    Author
    Replies
    • #1167385

      Can you describe the series of steps that you want to automate?

    • #1167699

      This is what I mean:

      1. Select a word or phrase in the text for which you want to substitute with a reference to a section heading (Heading 1,2,3…) of the same word or phrase.

      2. Give a shortcut command or click on a toolbar button run a macro

      3. The macro hunts for a section heading that exactly matches the same word or phrase. (See “Enhancements” below.)

      4. The macro inserts a hyperlink to the section heading. The hyperlink is inserted next to the text you selected (separated by a space) – this so the user can verify that the hyperlink is the same as the original text.

      Enhancements:

      – There may a number of exact matches, so the macro should display a list of all of them, each together with its section number. You can select the one you want

      – If no exact match is found then, the macro displays a list of near matches (whatever that means…)

      – It applies the hyperlink character style of your choice, e.g., to give a blue and underlined appearance.

      Thanks,

      – avi

      • #1167710

        We might get a shot at it if someone has a method to expose the list of Hyperlink targets through VBA. Anyone?

        • #1167715

          We might get a shot at it if someone has a method to expose the list of Hyperlink targets through VBA. Anyone?

          You can use the GetCrossReferenceItems method for that – see my reply to Avi.

      • #1167714

        Here is a macro that inserts a cross reference based on the selected text. It doesn’t implement the requested enhancements.

        Code:
        Sub AddXRef()
          Dim strText As String
          Dim arrHeadings As Variant
          Dim i As Integer
          strText = LCase(Selection.Text)
          arrHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
          For i = 1 To UBound(arrHeadings)
        	If Trim(LCase(arrHeadings(i))) = strText Then
        	  Selection.InsertAfter " - "
        	  Selection.Collapse Direction:=wdCollapseEnd
        	  Selection.InsertCrossReference _
        		ReferenceType:=wdRefTypeHeading, _
        		ReferenceKind:=wdContentText, _
        		ReferenceItem:=i, _
        		InsertAsHyperlink:=True
        	  Exit Sub
        	End If
          Next i
          MsgBox "Can't find a heading with text '" & strText & "'.", vbInformation
        End Sub
      • #1167725

        – It applies the hyperlink character style of your choice, e.g., to give a blue and underlined appearance.

        Applying the Hyperlink character style has no effect since a cross reference is a field. So you have to apply direct formatting:

        Code:
        Sub AddXRef()
          Dim strText As String
          Dim arrHeadings As Variant
          Dim i As Integer
          Dim lngStart As Long
          Dim lngEnd As Long
          strText = LCase(Selection.Text)
          arrHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
          For i = 1 To UBound(arrHeadings)
        	If Trim(LCase(arrHeadings(i))) = strText Then
        	  Selection.InsertAfter " - "
        	  Selection.Collapse Direction:=wdCollapseEnd
        	  lngStart = Selection.Start
        	  Selection.InsertCrossReference _
        		ReferenceType:=wdRefTypeHeading, _
        		ReferenceKind:=wdContentText, _
        		ReferenceItem:=i, _
        		InsertAsHyperlink:=True
        		lngEnd = Selection.Start
        		With ActiveDocument.Range(lngStart, lngEnd).Font
        		  .Color = wdColorBlue
        		  .Underline = wdUnderlineSingle
        		End With
        	  Exit Sub
        	End If
          Next i
          MsgBox "Can't find a heading with text '" & strText & "'.", vbInformation
        End Sub
        • #1167814

          Looks fantastic! Haven’t tried it though, yet.

          It would have taken me ages to get around to writing it myself.
          Maybe though it will spur me to add the enhancements.

          Er – what’s the difference between the two versions of the code?

          Thanks!

          – avi

          Applying the Hyperlink character style has no effect since a cross reference is a field. So you have to apply direct formatting:

          Code:
          Sub AddXRef()
            Dim strText As String
            Dim arrHeadings As Variant
            Dim i As Integer
            Dim lngStart As Long
            Dim lngEnd As Long
            strText = LCase(Selection.Text)
            arrHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
            For i = 1 To UBound(arrHeadings)
          	If Trim(LCase(arrHeadings(i))) = strText Then
          	  Selection.InsertAfter " - "
          	  Selection.Collapse Direction:=wdCollapseEnd
          	  lngStart = Selection.Start
          	  Selection.InsertCrossReference _
          		ReferenceType:=wdRefTypeHeading, _
          		ReferenceKind:=wdContentText, _
          		ReferenceItem:=i, _
          		InsertAsHyperlink:=True
          		lngEnd = Selection.Start
          		With ActiveDocument.Range(lngStart, lngEnd).Font
          		  .Color = wdColorBlue
          		  .Underline = wdUnderlineSingle
          		End With
          	  Exit Sub
          	End If
            Next i
            MsgBox "Can't find a heading with text '" & strText & "'.", vbInformation
          End Sub
          • #1167821

            Er – what’s the difference between the two versions of the code?

            The second version formats the cross reference fields with blue and underline.

        • #1167815

          “Applying the Hyperlink character style has no effect since a cross reference is a field. So you have to apply direct formatting:”

          Yes, I just happened to have noticed that within the past couple of weeks., i.e., that applying the Hyperlink character style has no effect since a cross reference is a field.
          What’s the reason for that?

          I got around the problem by creating a new character style of blue color and underlined, and I could successfully apply it to a field.

          – avi

          Applying the Hyperlink character style has no effect since a cross reference is a field. So you have to apply direct formatting:

          • #1167822

            applying the Hyperlink character style has no effect since a cross reference is a field.
            What’s the reason for that?

            I don’t have the slightest idea – one of Word’s quirks.

    Viewing 1 reply thread
    Reply To: Word 2003 VBA tool to find the targets of cross-references and insert them

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: