单词 VBA 搜索某些单词的句子
Word VBA search sentence for certain words
我正在编写一个小脚本来检查我的 word 文档。检查过程的一部分是检查我是否使用了被禁止的词。我在 MS Access 中创建了一个数据库,并将该数据库加载到 Word 中。
不起作用的部分实际上是检查是否使用了数据库中的某个单词。我循环每个句子并执行以下检查:
"RS" 是加载的数据库 table,"Selection" = 活动句,"Cword" 是禁用词的变量,在遍历数据库时发生变化table
'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 部分,因为我添加了该部分并且在它起作用之前。当我执行代码时,word 冻结,我必须强制关闭它。
由于这部分困扰了我很长时间,因此感谢任何帮助
问题可能是因为正在搜索的字词没有被删除或更改。因此,在每个循环中,搜索的术语 是 和 Selection
,所以它会一遍又一遍地找到相同的东西。 Word 没有冻结,它在 "infinite loop" 中。如果您按 Ctrl+Break,宏最终将停止执行,您可能会看到数百甚至数千条评论指向文档中的相同位置...
避免这种情况的方法是在下一个循环开始之前将选择移动到 "found" 项之外 - 就像按键盘上的右箭头一样。像这样:
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
您可以在整个文档上使用 Find/Replace 而不是循环遍历句子和进行选择,而无需选择任何内容:
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 表达式的区别;我的将找到以制表符、分段符或换行符之后的行开始的字符串,以及后跟任何这些或标点符号的字符串,以及空格 preceded/followed 的字符串。
我发现了问题。我忘了添加 RS.MoveNext
。如果没有这个,代码永远无法在第一个 while 循环中到达 End of Field
,因为数据库 table 包含多个项目。
我正在编写一个小脚本来检查我的 word 文档。检查过程的一部分是检查我是否使用了被禁止的词。我在 MS Access 中创建了一个数据库,并将该数据库加载到 Word 中。
不起作用的部分实际上是检查是否使用了数据库中的某个单词。我循环每个句子并执行以下检查:
"RS" 是加载的数据库 table,"Selection" = 活动句,"Cword" 是禁用词的变量,在遍历数据库时发生变化table
'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 部分,因为我添加了该部分并且在它起作用之前。当我执行代码时,word 冻结,我必须强制关闭它。
由于这部分困扰了我很长时间,因此感谢任何帮助
问题可能是因为正在搜索的字词没有被删除或更改。因此,在每个循环中,搜索的术语 是 和 Selection
,所以它会一遍又一遍地找到相同的东西。 Word 没有冻结,它在 "infinite loop" 中。如果您按 Ctrl+Break,宏最终将停止执行,您可能会看到数百甚至数千条评论指向文档中的相同位置...
避免这种情况的方法是在下一个循环开始之前将选择移动到 "found" 项之外 - 就像按键盘上的右箭头一样。像这样:
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
您可以在整个文档上使用 Find/Replace 而不是循环遍历句子和进行选择,而无需选择任何内容:
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 表达式的区别;我的将找到以制表符、分段符或换行符之后的行开始的字符串,以及后跟任何这些或标点符号的字符串,以及空格 preceded/followed 的字符串。
我发现了问题。我忘了添加 RS.MoveNext
。如果没有这个,代码永远无法在第一个 while 循环中到达 End of Field
,因为数据库 table 包含多个项目。