PDA

View Full Version : [SOLVED:] Opening web hyperlinks from Word doc: prevent macro stopping whenever it hits a 404



1819
02-16-2015, 11:31 AM
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.

gmaxey
02-16-2015, 02:24 PM
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

1819
02-17-2015, 03:50 PM
Many thanks Greg!

gmaxey
02-18-2015, 01:35 PM
You're welcome.