Search code examples
hyperlinkms-wordfind-replaceword-field

Can I find and replace multiple field codes with plain text, while retaining the value that the field refers to?


By 'the value which the field refers to', I am referring to the text which is between the { }.

What I have

Currently, I have many hyperlinks in pages of text like this:

Testing 123 and test 321.

What I hope for

I want to find and replace every instance of hyperlink field codes with plain text. Specifically, I want the ouput to look like this, in every case: *www.hyperlink.com

What I have tried

So far, I have been able to select the field in question, with the hyperlink by putting the following in the 'Find' form: HYPERLINK "*"

However, I am unsure what to put into the replace field in order to update the text as desired, with manually copying each link, then replacing with '^c' in each case. Since I have over 100 pages of link dense text, this is not feasible. I have also tried to use the wildcard and replace with the text: * but the wildcard doesn't seem to recognise that it is algebraically equivelant to the 'find' form's wildcard.

The closest I come is when I try: Find: HYPERLINK "(*)" Replace: \1 With the option 'use wildcards' checked.

While it moved the hyperlinks to between the ref tags, and banished the HYPERLINK text, the curly brackets { } where kept. So, when I switched out of form field mode (Alt F9) the www.ExampleHyperlink.com dissapears, because it's considered a Form Field thingo!

When I put the curly brackets in the 'find' field, the search fails - so I'm guessing I can't search for them, in order to replace them.

Possible code?

Sub Macro3()
'
' Macro3 Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "HYPERLINK ""(*)"""
        .Replacement.Text = "\1"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute replace:=wdReplaceAll
End Sub

Response to MacroPod

Thanks!

Now, I am able to banish the Hyperlink field code, and retain the hyperlink :)

I combine your macro with following macro/step to add the text and around each URL:

Sub Macro1()
'
' Macro1 Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "(<http://*.com>)"
        .Replacement.Text = "<ref>\1</ref>"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute replace:=wdReplaceAll
    With Selection.Find
        .Text = "(<www.*.com>)"
        .Replacement.Text = "<ref>\1</ref>"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute replace:=wdReplaceAll
End Sub

It works almost perfectly, except that any part of the URL after the '.com' is missed. For instance, if the URL is: 'https://www.google.com/intl/en_us/health/about/' then the output is:

'<ref> https://www.google.com </ref> /intl/en_us/health/about/' 

which is not quite what I'm going for because I'd like the second ref tag to come after the last part of the URL. Another issue is that whatever the anchor text was that linked to the URL dissapears. Ideally, it would come immediately prior to the tag. That's because something is usually linked like this: a cooldog is always cool. I really appreciate your assistance. Any idea how I can deal with this?


Solution

  • Try:

    Sub Demo()
    With ActiveDocument
      While .Hyperlinks.Count > 0
        .Hyperlinks(1).Range.InsertBefore .Hyperlinks(1).Address
        .Hyperlinks(1).Delete
      Wend
    End With
    End Sub