Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Word 2010 - Macro to replace hyperlinks with PDF URLs

    Hi all,

    I'm very much a newbie to VBA, but have usually been able to cobble together what I need by studying similar snippets of code and editing the relevant parts. But I've hit a wall with this latest code and would be very grateful for any help.

    I have about 50 Word documents containing unique hyperlinks that each need to be replaced with a unique updated hyperlink. Each URL is different but they follow the same general style:

    The original URL format is:
    HTML Code:
    http://www.sampledomain.com/articles/section/YY_MM_DD_Name_of_Article.pdf
    The updated URL format is:
    HTML Code:
    http://www.sampledomain.com/index.php?id=somenumber
    I tried showing field codes and running Find & Replace, but this only changed the text of the field code link and not the displayed text or the actual URL. Next I tried working with this code (this is only a snippet - the full code included all 50 of the URL match-ups):

    Code:
    Sub ReplacingLinks()
    '
    '
    Dim doc As Document
    Dim link, i
    'Loop through all open documents.
    For Each doc In Application.Documents
        'Loop through all hyperlinks.
        For i = 1 To doc.Hyperlinks.Count
            'If the hyperlink matches.
            If LCase(doc.Hyperlinks(i).Address) = "http://www.sampledomain.com/articles/dancing/11_10_26_Sample_Title.pdf" Then
                'Change the links address.
                doc.Hyperlinks(i).Address = "http://www.sampledomain.com/index.php?id=275"
            End If
    
            If LCase(doc.Hyperlinks(i).Address) = "http://www.sampledomain.com/articles/dogs/11_11_09_Another_Sample_Title.pdf" Then
                'Change the links address.
                doc.Hyperlinks(i).Address = "http://www.sampledomain.com/index.php?id=113"
            End If
    
            If LCase(doc.Hyperlinks(i).Address) = "http://www.sampledomain.com/articles/dogs/11_11_23_Paper_and_Pencils_and_Staplers.pdf" Then
                'Change the links address.
                doc.Hyperlinks(i).Address = "http://www.sampledomain.com/index.php?id=15"
            End If
    
    Next
    Next
    End Sub
    But for some reason, this doesn't work with the original PDF URLs. When I run the macro, nothing happens. If I change the original hyperlink to, for example, "http://www.sampledomain.com/articles/dogs/", the address gets changed correctly. But the full PDF link doesn't register, and I don't know what it is about these URLs that isn't working.

    Can anyone help point me in the right direction or have suggestions for an alternate approach? Many thanks in advance.
    Last edited by silvergreen; 2014-02-24 at 12:48.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,535
    Thanks
    3
    Thanked 147 Times in 140 Posts
    I am wondering why you think the LCase command might return a string with capital letters in it.

    I would also shorten it by using the Replace function
    Code:
    Dim aHL as Hyperlink
    For each aHL in doc.Hyperlinks
       aHL.Address = Replace(LCase(aHL.Address), "articles/dancing/11_10_26_sample_title.pdf", "index.php?id=275"
       aHL.Address = Replace(LCase(aHL.Address), "articles/dogs/11_11_09_another_sample_title.pdf", "index.php?id=113"
       aHL.Address = Replace(LCase(aHL.Address), "articles/dogs/11_11_23_paper_and_pencils_and_staplers.pdf", "index.php?id=15"
    Next aHL
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    silvergreen (2014-02-24)

  4. #3
    New Lounger
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Wow. Head, meet desk. Thank you for pointing that out!

Posting Permissions

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