• Find and Replace Hyperlinks in Word 2K

    Author
    Topic
    #353304

    Hi All

    I have a 520 page Word document in which I have a good 300 odd external hyperlinks (12 hyperlinks x 30 times each in the document).

    Each hyperlink needs to be altered 30 times to an affiliate URL in the document before the document is compiled as a PDF.

    Unfortunately, the only way I can see to do this is by right-clicking each link and editing it as Word’s Find and Replace only changes the link as it’s displayed – *not* the underlying hyperlink itself.

    For example:

    If you run a Find and Replace on http://www.microsoft.com with http://www.netscape.com, the actual hyperlink will still be Microsoft’s.

    Does anyone know of a way I can batch F&R all instances of a hyperlink and display text in a document in one go?

    At the moment, I have to convert the Word doc to HTML, perform F&R and then convert it back before compiling thru Distiller.

    Surely, there’s got to be a better way than this?

    All help gratefully received.

    Cheers

    Nick

    Viewing 1 reply thread
    Author
    Replies
    • #516370

      You can replace them by toggling field codes on, to replace the actual link; and field codes off, to replace the link text.

      Tools-Options-View-Field Codes

      • #516378

        And with field codes not displayed, you can search for the hyperlink field code anyway in case you’ll need to edit the displayed text by hand.

        • #516528

          Thanks to everybody who answered my call, especially Gary for the macro.

          If I can ever repay the favour, please email me!

          Cheers

          Nick

    • #516435

      Hi Nick,

      The following code illustrates how to do the find and replaces you need, using a macro (if I understand your requirements correctly).

      Also, attached is a sample file that this code was designed to run on (the file contains the code as well).

      As it stands, this is still a little labor-intensive in that you would need to hard-code all the find and replace strings for the text and addresses of each of the 12 hyperlinks (still beats doing the whole document manually). Perhaps a clever way can be found to automate this part of it as well.

      Hope this helps,
      Gary

      Code:
      Sub FindReplaceHyperlinks()
      'Gary Frieder  February 2001
      Dim objDocHLinks As Hyperlinks
      Dim lngHypCt As Long
      Dim n As Long
      Dim strHLOrigText As String
      Dim strHLOrigAddress As String
      Dim strHLNewText As String
      Dim strHLNewAddress As String
      
      Set objDocHLinks = ActiveDocument.Hyperlinks
      lngHypCt = objDocHLinks.Count
      For n = lngHypCt To 1 Step -1
          strHLOrigText = objDocHLinks(n).Range.Text
          'call function to get replacement text
          strHLNewText = GetNewHLText(strHLOrigText)
          
          strHLOrigAddress = objDocHLinks(n).Address
          'call function to get replacement address
          strHLNewAddress = GetNewHLAddress(strHLOrigAddress)
              
          With objDocHLinks(n).Range
              .Fields(1).Result.Select
              .Delete
          End With
          Selection.Text = strHLNewText
          objDocHLinks.Add Anchor:=Selection.Range, Address:=strHLNewAddress
          Selection.Collapse Direction:=wdCollapseEnd
      Next n
      End Sub
      '------------------------------------
      Public Function GetNewHLText(strOrigText As String) As String
      Dim strNewText As String
      Select Case strOrigText
          Case "firstHLink"
          strNewText = "firstHLinkReplacementText"
          Case "secondHLink"
          strNewText = "secondHLinkReplacementText"
          Case "thirdHLink"
          strNewText = "secondHLinkReplacementText"
      End Select
      GetNewHLText = strNewText
      End Function
      '------------------------------------
      Public Function GetNewHLAddress(strOrigAddress As String) As String
      Dim strNewAddress As String
      Select Case strOrigAddress
          Case "http://www.accuweather.com/"
          strNewAddress = "http://www.weather.com/"
          Case "http://www.cnn.com/"
          strNewAddress = "http://www.cbs.com/"
          Case "http://www.amazon.com/"
          strNewAddress = "http://www.barnesandnoble.com/"
      End Select
      GetNewHLAddress = strNewAddress
      End Function
    Viewing 1 reply thread
    Reply To: Find and Replace Hyperlinks in Word 2K

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

    Your information: