【问题标题】:Word VBA search sentence for certain wordsWord VBA搜索某些单词的句子
【发布时间】:2018-12-22 13:21:49
【问题描述】:

我正在编写一个小脚本来检查我的 word 文档。检查过程的一部分是检查我是否使用了被禁止的词。我在 MS Access 中创建了一个数据库,并将该数据库加载到 Word 中。

不起作用的部分实际上检查是否使用了数据库中的单词之一。我正在循环每个句子并执行以下检查:

"RS" 是加载的数据库表,"Selection" = 活动句子,"Cword" 是禁用词的变量,在循环访问数据库表时会发生变化

        'Word check
        RS.MoveFirst
        While Not RS.EOF
            Cword = LCase(RS!Woord)
            PCword = LCase(RS!Pre)

            With Selection.Range.Find
            .ClearFormatting
            .MatchWildcards = True
                While .Execute(FindText:=" " & Cword & " ", Forward:=True)
                    If .Found = True Then
                        Oms = RS!Omschrijving
                        ActiveDocument.Comments.Add(Selection.Range, Oms).Author = ComAut
                        VB_count = VB_count + 1
                        RS.MoveLast
                    End If
                Wend
            End With
        Wend

        'number check
        With Selection.Range.Find
            .ClearFormatting
            .MatchWildcards = True
            While .Execute(FindText:=" [0-9] ", Forward:=True)
                If .Found = True Then
                    SingleNumber = Selection.Range
                    Tientallen = SingleNumber Mod 10
                    Hondertallen = SingleNumber Mod 100
                    Duizendtallen = SingleNumber Mod 1000
                    If SingleNumber <= 0 Or Tientallen = 0 And SingleNumber <= 100 Or Hondertallen = 0 And SingleNumber <= 1000 Or Duizendtallen = 0 And SingleNumber <= 12000 Then
                        ActiveDocument.Comments.Add(Selection.Range, "dit getal bij voorkeur voluit schrijven. Uitgezonderd van bijvoorbeeld leeftijden, exacte waarden, maten, temperaturen en percentages").Author = ComAut
                    End If
                End If
            Wend
        End With

我希望它做的是查找句子中的每个禁用词,如果找到,添加带有小描述的评论。问题出在 with find 部分,因为我添加了该部分并且在它起作用之前。当我执行代码时,单词会冻结,我必须强制关闭它。

感谢任何帮助,因为这部分困扰了我很长时间

【问题讨论】:

    标签: vba ms-word find wildcard


    【解决方案1】:

    问题可能来自于正在搜索的术语没有被删除或更改。因此,在每个循环中,被搜索的词Selection,所以它不断地寻找相同的东西,一遍又一遍。 Word 没有冻结,它处于“无限循环”中。如果您按下 Ctrl+Break,宏最终会停止执行,您可能会看到数百甚至数千个 cmets 指向文档中的同一位置...

    避免这种情况的方法是在下一个循环开始之前将选择移动到“找到”项之外 - 就像按键盘上的右箭头一样。像这样的:

            With Selection.Range.Find
              .ClearFormatting
              .MatchWildcards = True
              .Wrap = wdFindStop  'Prevent Word from starting again at the beginning of the document
                While .Execute(FindText:=" " & Cword & " ", Forward:=True)
                    If .Found = True Then 'Not really necessary since the "While" already checks this...
                        Oms = RS!Omschrijving
                        ActiveDocument.Comments.Add(Selection.Range, Oms).Author = ComAut
                        VB_count = VB_count + 1
                        RS.MoveLast
                        Selection.Collapse wdCollapseEnd 'like pressing right-arrow key
                    End If
                Wend
            End With
    

    【讨论】:

    • 嘿,我找到了问题,看我对这个话题的回答。感谢您的帮助。您的回答仍然很有价值,因为我不知道 .wrap 和 collapse 属性
    【解决方案2】:

    您可以在不选择任何内容的情况下对整个文档使用查找/替换,而不是遍历句子并进行选择:

    Cword = LCase(RS!Woord)
    Oms = RS!Omschrijving
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "<" & Cword & ">"
        .Replacement.Text = ""
        .Forward = True
        .Format = False
        .Wrap = wdFindStop
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        .Comments.Add(Range:=.Range, Text:=Oms).Author = ComAut
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    RS.MoveLast
    

    还要注意通配符 Find 表达式的区别;我的会找到在制表符、分节符或换行符之后开始行的字符串,以及后面跟任何这些或标点符号的字符串,以及前面/后面有空格的字符串。

    【讨论】:

    • 嘿,我找到了问题,看我的回答。还是谢谢你的回答。我无法在整个文档中搜索特定的单词,因为我正在循环的数据库包含多个项目。此外,我还对每个句子进行了一些检查,这就是我循环遍历每个句子而不是整个文档的原因。
    • @Vinc199789 有多个项目这一事实并不能否定我的代码所采用的方法;即使您出于其他目的需要这种方法,它仍然比基于句子的循环更有效。至于循环句子,你需要知道 VBA 不知道什么是语法句子。例如,考虑以下情况:Mr.史密斯在约翰博士的杂货店花了 1,234.56 美元购买: 10.25 公斤土豆; 10公斤鳄梨;和15.1公斤格林夫人的Mt. Pleasant澳洲坚果。 对你我来说,这就是一句话;对于 VBA,它算作 5 个句子。
    • 我将脚本更改为基于文档的循环而不是基于句子的循环,并且它执行检查部分的速度更快。谢谢你的提示。没想到会这么快。
    【解决方案3】:

    我发现了问题。我忘了加RS.MoveNext。没有这个,代码在第一个 while 循环中永远无法到达End of Field,因为数据库表包含多个项目。

    【讨论】:

      猜你喜欢
      • 2021-06-22
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多