Results 1 to 11 of 11
  1. #1
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Can you describe the series of steps that you want to automate?

  3. #3
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  4. #4
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post
    We might get a shot at it if someone has a method to expose the list of Hyperlink targets through VBA. Anyone?

    [attachment=84560:HyperlinkList.GIF]
    Attached Images Attached Images
    Regards
    Don

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    [quote name='wdwells' post='783125' date='05-Jul-2009 20:43']We might get a shot at it if someone has a method to expose the list of Hyperlink targets through VBA. Anyone?[/quote]
    You can use the GetCrossReferenceItems method for that - see my reply to Avi.

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    [quote name='amakeler' post='783112' date='05-Jul-2009 18:11']- It applies the hyperlink character style of your choice, e.g., to give a blue and underlined appearance.[/quote]
    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

  8. #8
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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


    [quote name='HansV' post='783140' date='05-Jul-2009 20:55']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
    [/quote]

  9. #9
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    "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



    [quote name='HansV' date='05-Jul-2009 20:55' post='783140']
    Applying the Hyperlink character style has no effect since a cross reference is a field. So you have to apply direct formatting:

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    [quote name='amakeler' post='783252' date='06-Jul-2009 22:29']Er - what's the difference between the two versions of the code?[/quote]
    The second version formats the cross reference fields with blue and underline.

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    [quote name='amakeler' post='783253' date='06-Jul-2009 22:33']applying the Hyperlink character style has no effect since a cross reference is a field.
    What's the reason for that?[/quote]
    I don't have the slightest idea - one of Word's quirks.

Posting Permissions

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