Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Feb 2001
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find and Replace Hyperlinks in Word 2K

    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

  2. #2
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Massachusetts
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and Replace Hyperlinks in Word 2K

    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

  3. #3
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Madison, Wisconsin, Wisconsin, USA
    Posts
    1,708
    Thanks
    57
    Thanked 65 Times in 63 Posts

    Re: Find and Replace Hyperlinks in Word 2K

    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.
    Charles Kyle Kenyon
    Madison, Wisconsin

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 28 Times in 27 Posts

    Re: Find and Replace Hyperlinks in Word 2K

    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
    Attached Files Attached Files
    Last edited by macropod; 2012-12-08 at 21:53. Reason: Fixed code tags for new lounge

  5. #5
    New Lounger
    Join Date
    Feb 2001
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and Replace Hyperlinks in Word 2K

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

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

    Cheers

    Nick

Posting Permissions

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