rayvd
rayvd

Reputation: 141

In VBA for Word, How can I use the Find method on a hyperlink address?

I have 700+ links in a document, and I want to scan the hyperlink addresses with a certain simple regex using the .Find method. Unfortunately, everything I try limits the Find to the displayed text rather than the actual hyperlink address.

Any help would be appreciated. This is a code snippet that represents what I have now:

Set rng = .Hyperlinks(i).Range
Set fnd = rng.Find

(The range set in the first line, is unfortunately the displayed text, and not the hyperlink address text.)

Thank you.

Upvotes: 1

Views: 168

Answers (1)

M--
M--

Reputation: 28826

You can set the FieldCode to make the links visible:

ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes

Then you can search for links as same as what you do for texts; This's just an example that I recorded:

ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
CommandBars("Navigation").Visible = False
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "https://me.you"
    .Replacement.Text = "https://you.me"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With

Upvotes: 0

Related Questions