MS Word VBA-查找单词并更改其样式 [英] MS Word VBA - Finding a word and changing its style
问题描述
我正在尝试查找MS Word文档中关键字的所有实例并更改其样式.关键字存储在数组中,我只想更改特定单词的样式.理想情况下,这会在我键入内容时发生,但这并不重要.
I'm trying to find all instances of key words in a MS Word document and change their style. The key words are stored within an array and I want to change the style of the particular word only. Ideally this would happen as I type but that is not crucial.
尝试1-基于记录宏并更改搜索字词
Attempt 1 - Based on recording a macro and changing the search term
Sub Woohoo()
Dim mykeywords
mykeywords= Array("word1","word2","word3")
For myword= LBound(mykeywords) To UBound(mykeywords)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("NewStyle")
With Selection.Find
.Text = mykeywords(myword)
.Replacement.Text = mykeywords(myword)
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next
End Sub
这将更改单词所在的整个段落的样式.
This changes the style of the entire paragraph where the words are in.
Attempt 2 - Based on this question here How can I replace a Microsoft Word character style within a range/selection in VBA?:
Sub FnR2()
Dim rng As Range
Dim mykeywords
mykeywords = Array("word1","word2","word3")
For nKey = LBound(mykeywords) To UBound(mykeywords)
For Each rng In ActiveDocument.Words
If IsInArray(rng, mykeywords(nKey)) Then
rng.Style = ActiveDocument.Styles("NewStyle")
End If
Next
Next
End Sub
这会查找单行中的单词,但由于某种原因(例如,由于某种原因)会跳过段落中的单词.它找到
This finds words that are in single lines but skips the words that are within a paragraph for some reason, e.g. it finds
Some text
word1
more text
但不是
Some text before word1 means that the code above doesn't change the format
Word1 also isn't changed in this instance
尝试3-自动更正;实际没有尝试过:
Attempt 3 - AutoCorrect; not actually tried:
作为替代方案,我正在考虑使用自动更正.但是,我有100多个关键字,并且不知道如何将其自动添加到自动更正"列表中(我相当是VBA文盲).我会用这种方法看到的另一个问题是,我认为自动更正"是全局的,而我只需要对特定文档起作用.
As an alternative I was thinking to use AutoCorrect. However I have more than 100 keywords and have no idea how to add this to the AutoCorrect list automatically (I'm fairly VBA illiterate). The other problem I would see with this approach is that I believe that AutoCorrect is global, whereas I need this only to work for a specific document.
推荐答案
我相信您的宏找不到单词的原因是由于前导或尾随空格的存在.前提是您已经定义了样式"NewStyle",将
I believe the reason why your macro isn't finding the words is due to the presence of leading or trailing blank spaces. Providing that you have already defined the style "NewStyle" changing your if statement in SubFnR2 from
If IsInArray(rng, mykeywords(nKey)) Then
到
If mykeywords(nkey) = LCase(Trim(rng.Text)) Then
应该解决问题.顺便说一句,如果您想根据大小写来保留单词的样式,请删除LCase部分.
Should solve the issue. By the way if you want to keep the style of the word depending on whether it is upper or lower case then remove the LCase part.
我在下面的修改中包含了该子.我已经在您提供的示例(剪切并粘贴到word)中对其进行了测试,它改变了两个实例word1的样式.
I have included the sub with the modification below. I have tested it on the examples you gave (cut and pasted into word) and it changed the style for both instances word1.
Sub FnR3()
Dim rng As Range
Dim mykeywords
mykeywords = Array("word1", "word2", "word3")
Dim nkey As Integer
For nkey = LBound(mykeywords) To UBound(mykeywords)
For Each rng In ActiveDocument.Words
If mykeywords(nkey) = LCase(Trim(rng.Text)) Then
rng.Style = ActiveDocument.Styles("NewStyle")
End If
Next rng
Next nkey
End Sub
好的,您的文档的行为与您描述的一样,我不太清楚为什么.我检查了选择范围,只选择了单词,但随后整个段落都被格式化了.我已经修改了代码以修改选择,如下所示.这确实改变了这个词.
Ok, your document behaves has you described, I'm not quite sure why. I checked selecting the range and just the word was selected, but then the whole paragraph was formatted. I have modified the code to modify the selection, shown below. This did just change the word.
Sub FnR4()
Dim rng As Range
Dim mykeywords
mykeywords = Array("word1", "word2", "word3")
Dim nkey As Integer
For nkey = LBound(mykeywords) To UBound(mykeywords)
For Each rng In ActiveDocument.Words
Selection.Collapse
rng.Select
If mykeywords(nkey) = LCase(Trim(rng.Text)) Then
Selection.Style = ActiveDocument.Styles("NewStyle")
End If
Next rng
Next nkey
End Sub
这篇关于MS Word VBA-查找单词并更改其样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!