遍历段落中的每个单词VBA
我想遍历段落中的每个单词,我编写代码:遍历段落中的每个单词VBA,vba,ms-word,Vba,Ms Word,我想遍历段落中的每个单词,我编写代码: sub search_word_in_paragraph() With selection.Find .Text = "[A-Za-z]{2,}" .MatchWildcards = True Do .Execute If .Found Then If select
sub search_word_in_paragraph()
With selection.Find
.Text = "[A-Za-z]{2,}"
.MatchWildcards = True
Do
.Execute
If .Found Then
If selection.Range.Text = "and" Then
selection.Range.Case = wdLowerCase
Exit For
End If
End If
Loop Until selection.Range.Previous <> " "
selection.Range.Case = wdTitleWord
selection.HomeKey unit:=wdLine
End With
end sub
段落()中的子搜索单词
选择。查找
.Text=“[A-Za-z]{2,}”
.MatchWildcards=True
做
.执行
如果。找到了
如果selection.Range.Text=“and”,则
selection.Range.Case=wd小写
退出
如果结束
如果结束
循环直到selection.Range.Previous“”
selection.Range.Case=wdTitleWord
selection.HomeKey单位:=wdLine
以
端接头
但是,我觉得可以更简洁一些,还有其他更好的方法吗?您可以使用
For…For…Each
遍历段落中的单词。下面是一个例子
Sub TraverseWords()
Dim rng As Range
Set rng = Selection.Paragraphs(1).Range
For Each wrd In rng.Words
If Trim(wrd) = "and" Then
wrd.Case = wdLowerCase
Exit For
End If
Next
End Sub
您可以使用
For…Each
遍历段落中的单词。下面是一个例子
Sub TraverseWords()
Dim rng As Range
Set rng = Selection.Paragraphs(1).Range
For Each wrd In rng.Words
If Trim(wrd) = "and" Then
wrd.Case = wdLowerCase
Exit For
End If
Next
End Sub
非常感谢,这是我想要的,但是为什么rng中的每个单词都是
。单词
不正确,word和wrd之间的区别是什么单词
指的是单词应用程序
,所以你不能使用它。非常感谢,这是我想要的,但是rng中的每个单词都是。单词
不正确,word和wrd的区别是什么?word指的是word应用程序,因此您不能使用它。