【问题标题】:Find / Replace text using string found within Word document使用 Word 文档中的字符串查找/替换文本
【发布时间】:2014-11-27 15:11:49
【问题描述】:

我一直在尝试修改here 给出的出色示例,但收效甚微。在 MSWord 文档中,我需要能够找到像 <<TEST>> 这样的文本并恢复在 <<>> 之间找到的字符串,该字符串将返回 TEST。最终,我打算使用它来查找 TEST 的值并返回要在 Word 文档中替换的字符串。 IE。例如,<<TEST>> 变为 FRED

Sub Sample()
    Dim c As Range
    Dim StartWord As String, EndWord As String, TheWord As String

    StartWord = "<<": EndWord = ">>"

    Set c = ActiveDocument.Content
    c.Find.ClearFormatting
    c.Find.Replacement.ClearFormatting
    With c.Find
        .Text = "[\<]{2}*[\>]{2}"
        '.Replacement.Text = TheWord
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    c.Find.Execute
    While c.Find.Found
        Debug.Print c.Text
        TheWord = Replace(Replace(c.Text, StartWord, ""), EndWord, "")
        Debug.Print TheWord
        c.Find.Replacement.Text = TheWord
        ' Future something here to lookup value based on 'TheWord'
        c.Find.Execute Replace:=wdReplaceOne
    Wend
End Sub

目前,我只是想用在其中找到的字符串替换那些像&lt;&lt;TEST&gt;&gt; 这样的词。虽然它会找到并替换与模式匹配的文本的第一个实例,但它不会找到像 example 这样的其他实例。

谢谢。

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    即使有时不建议在您的代码中使用Selection,但我更喜欢在运行find &gt;&gt; replace 操作时使用它。

    在以下代码中,您将找到两种解决方案 - 第一个是将文本替换为 &lt;&lt; &gt;&gt; 括号内的一个,第二个是替换为任何文本.不要同时运行两个,注释一个以运行另一个。

    Sub Sample()
        Dim c As Range
        Dim StartWord As String, EndWord As String, TheWord As String
    
        StartWord = "<<": EndWord = ">>"
    
        ActiveDocument.Range(0, 0).Select
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
    
        With Selection.Find
            .Text = "[\<]{2}(*)[\>]{2}"
            .Replacement.Text = "\1"
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
    
        'DO NOT RUN BOTH OPTIONS TOGETHER, CHOOSE ONE
    
        'OPTION 1. replace to inside text
        'Selection.Find.Execute Replace:=wdReplaceAll
    
    
        'OPTION 2. replace to any text, here- inside text found with replace function
        Do While Selection.Find.Execute
    
            Debug.Print Selection.Text
            TheWord = Replace(Replace(Selection.Text, StartWord, ""), EndWord, "")
            Debug.Print TheWord
            Selection.Text = TheWord
            Selection.Collapse WdCollapseDirection.wdCollapseEnd
    
        Loop
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2022-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 2019-05-20
      • 2021-08-11
      • 2021-10-13
      • 2015-01-31
      相关资源
      最近更新 更多