Consulting

Results 1 to 4 of 4

Thread: Opening web hyperlinks from Word doc: prevent macro stopping whenever it hits a 404

  1. #1
    VBAX Regular
    Joined
    Oct 2014
    Posts
    95
    Location

    Opening web hyperlinks from Word doc: prevent macro stopping whenever it hits a 404

    This simple code opens selected links in a browser:

    Sub AAOpenSelectedLinks()
    Dim oLink As Hyperlink
    For Each oLink In Selection.Hyperlinks
    oLink.Follow
    Next
    End Sub
    However, I get "Runtime error '4198' caused at the line

    oLink.Follow
    and an error message "Internet site reports that the item you requested cannot be found (HTTP/ 1.0 404)" when the weblink no longer exists.

    Please could you advise how to the amend the code so that if the macro finds a 404 error, it skips the link and move to the next one.

    Thanks.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Handle the errors:

    Sub AAOpenSelectedLinks()
      Dim oLink As Hyperlink
      On Error Resume Next
      For Each oLink In Selection.Hyperlinks
        oLink.Follow
      Next
      On Error GoTo 0
    End Sub
    'or
    Sub AAOpenSelectedLinksII()
      Dim oLink As Hyperlink
      On Error GoTo Err_Link
      For Each oLink In Selection.Hyperlinks
        oLink.Follow
      Next
      Exit Sub
    Err_Link:
      MsgBox "Error " & Err.Description
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Oct 2014
    Posts
    95
    Location
    Many thanks Greg!

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    You're welcome.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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