2

我想在整个 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

4

2 回答 2

3

尝试这个。

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
于 2012-07-14T17:53:18.020 回答
0

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

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

如我错了请纠正我。

于 2019-08-26T16:44:21.423 回答