2
votes

I want to create a Macro that searches the Word document for all hyperlinks. If the visible text begins with http, then I want to remove the link (leave the text as is).

Example: If the following 2 links are in my word document, the first would be unlinked, but the second one would retain it's link.

http://www.google.com

Link to Google

I don't know how to pull the text value from the Field element so that I can compare. This is what my macro currently looks like:

Sub RemoveHyperlinks()
  Dim oField As Field
  For Each oField In ActiveDocument.Fields
    If oField.Type = wdFieldHyperlink Then
      'If the text of oField begins with "http" then unlink
      oField.Unlink
    End If
  Next
  Set oField = Nothing
  End Sub
1

1 Answers

2
votes

You can get the display value of a field using the Resultproperty:

Sub RemoveHyperlinks()
  Dim oField As Field
  For Each oField In ActiveDocument.Fields
    If oField.Type = wdFieldHyperlink Then
      If Left(oField.Result, 4) = "http" then
        oField.Unlink
      End If
    End If
  Next
  Set oField = Nothing
End Sub