如何将搜索字符串转换为字段?

如何将搜索字符串转换为字段?

我有一个文档,其中多次出现INCLUDEPICTURE "imagename.png"。我不想选择文本并按 Ctrl+F9,而是想自动执行此操作。

是否可以将模式转换INCLUDEPICTURE*png为字段?

答案1

使用 Word 的搜索和替换无法做到这一点,因为 ^d(查找字段)不能与通配符一起使用,但此宏可以做到这一点:

Sub ConvertToField()
    Do
        With Selection.Find
            .Text = "INCLUDEPICTURE ""*.png"""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        If Selection.Find.Found = False Then Exit Do

        Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, PreserveFormatting:=False
        Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
        Selection.MoveRight Unit:=wdWord, Count:=1

    Loop While True
End Sub
Sub ConvertToText()
    Do
        With Selection.Find
            .Text = "^d"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        If Selection.Find.Found = False Then Exit Do

        If Selection Like "* INCLUDEPICTURE *.png*" Then
            Selection = Mid(Selection, 3, Len(Selection) - 2 - 2)
            Selection.Move wdCharacter, 1
        End If
    Loop While True
End Sub

相关内容