【问题标题】:Search text with Content.Find object and recover the text found使用 Content.Find 对象搜索文本并恢复找到的文本
【发布时间】:2012-07-14 02:00:14
【问题描述】:

我想在整个 MSWord 文档中搜索带有通配符的文本并恢复找到的字符串。

类似的东西:

Sub Macro1()
    Dim c As Range
    Set c = ActiveDocument.Contentsdf
    c.Find.ClearFormatting
    c.Find.Replacement.ClearFormatting
    With c.Find
        .Text = "start[abcde]end"
        .Replacement.Text = ""
        .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.Find.TextFound
        c.Find.Execute
    Wend
End Sub

但方法c.Find.TextFound 不存在。有没有办法恢复文本而不重复到Selection.Text

【问题讨论】:

  • 您要搜索*abcde* 还是start*end
  • 我想搜索以“start”开头、以“end”结尾并包含“a”、“b”、“c”、“d”、“e”字母的字符串中间。
  • 你能举个例子吗? start[a]endstart[asd]endstartaendstartasdend
  • 我知道这可以使用 Find.Text = "start[abcde]end" 来完成,并且我可以使用 Selection.Text 遍历找到的每个匹配项来恢复文本。但是我正在修改其他人通过 WIN32::OLE 使用 VBA 的 perl 编写的代码,并且我无法访问 Word.Selection 对象(我需要修改很多代码才能做到这一点),所以我想知道如果有任何方法可以恢复从 Find 对象中找到的文本。

标签: vba ms-word find


【解决方案1】:

试试这个。

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

    StartWord = "Start": EndWord = "End"

    Set c = ActiveDocument.Content
    c.Find.ClearFormatting
    c.Find.Replacement.ClearFormatting
    With c.Find
        .Text = StartWord & "*" & EndWord
        .Replacement.Text = ""
        .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
        '~~> I am assuming that the start word and the end word will only
        '~~> be in the start and end respectively and not in the middle
        Debug.Print Replace(Replace(c.Text, StartWord, ""), EndWord, "")
        c.Find.Execute
    Wend
End Sub

【讨论】:

    【解决方案2】:

    我一直在寻找一种方法来打印一定数量的页面,只要它有一个特定的单词。

    基本上我有 3 个文件,我必须打印它们并按班次组织它们。它们中的每一个都作为 Shift 1 2 和 3 放在它们上面。我设法一次将它们全部打印,但我想从第一个文件打印 Shift 1,从第二个文件打印 2,依此类推,直到文件 5 的 shift 3。我只需要一个宏代码来搜索单词“ shift 1" 并仅打印包含该单词的页面。然后基本上是对所有文件应用相同的代码,将 1 更改为 2 和 3。

    如果我错了,请纠正我。

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多