我能否找到多个域代码并将其替换为纯文本,同时保留该域引用的值?

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

'the value which the field refers to',我指的是 { } 之间的文本。

我有什么

目前,我在这样的文本页面中有很多 hyperlink:

Testing 123 and test 321.

我所希望的

我想查找每个 hyperlink 域代码实例并将其替换为纯文本。具体来说,我希望输出看起来像这样,在每种情况下:*www.hyperlink.com

我试过的

到目前为止,我已经能够 select 有问题的字段,通过将以下内容放入 'Find' 表单中,使用 hyperlink:HYPERLINK "*"

但是,我不确定要在替换字段中放入什么以便根据需要更新文本,手动复制每个 link,然后在每种情况下替换为“^c”。由于我有超过 100 页的 link 密集文本,这是不可行的。我还尝试使用通配符并替换为文本:* 但通配符似乎无法识别它在代数上等同于 'find' 形式的通配符。

我最接近的是当我尝试: 查找:超链接“(*)” 替换:\1 选中选项 'use wildcards'。

虽然它将 hyperlinks 移动到 ref 标签之间,并消除了 HYPERLINK 文本,但保留了大括号 { }。因此,当我退出表单字段模式 (Alt F9) 时,www.ExampleHyperlink.com 消失了,因为它被认为是表单字段的东西!

当我将花括号放在 'find' 字段中时,搜索失败 - 所以我猜我无法搜索它们以替换它们。

可能的代码?

Sub Macro3()
'
' Macro3 Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "HYPERLINK ""(*)"""
        .Replacement.Text = ""
        .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

响应 MacroPod

谢谢!

现在,我可以去掉 Hyperlink 字段代码,并保留 hyperlink :)

我将您的宏与以下 macro/step 结合起来以添加文本并围绕每个 URL:

Sub Macro1()
'
' Macro1 Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "(<http://*.com>)"
        .Replacement.Text = "<ref></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></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

它的工作几乎完美,除了 URL 在 '.com' 之后的任何部分都被遗漏了。例如,如果 URL 是:'https://www.google.com/intl/en_us/health/about/' 那么输出是:

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

这不是我想要的,因为我希望第二个 ref 标签出现在 URL 的最后一部分之后。另一个问题是,无论 link 编辑到 URL 的锚文本是什么,它都会消失。理想情况下,它会紧接在标签之前。那是因为通常 link 是这样的:cooldog 总是很酷。非常感谢您的帮助。知道我该如何处理吗?

尝试:

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