Reputation: 995
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.
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
Upvotes: 2
Views: 1125
Reputation: 25663
You can get the display value of a field using the Result
property:
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
Upvotes: 2