简体   繁体   English

MS Word VBA-查找单词并更改其样式

[英]MS Word VBA - Finding a word and changing its style

I'm trying to find all instances of key words in a MS Word document and change their style. 我正在尝试在MS Word文档中查找关键字的所有实例并更改其样式。 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. 理想情况下,这会在我键入内容时发生,但这并不重要。

Attempt 1 - Based on recording a macro and changing the search term 尝试1-基于记录宏并更改搜索词

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? 尝试2-基于此问题, 如何在VBA中的范围/选择范围内替换Microsoft Word字符样式? :

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, eg it finds 这会查找单行中的单词,但由于某种原因会跳过段落中的单词,例如

Some text
word1
more text

but not 但不是

Some text before word1 means that the code above doesn't change the format
Word1 also isn't changed in this instance

Attempt 3 - AutoCorrect; 尝试3-自动更正; not actually tried: 实际没有尝试过:

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). 但是我有100多个关键字,并且不知道如何将其自动添加到“自动更正”列表中(我相当是VBA文盲)。 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. 我会用这种方法看到的另一个问题是,我认为“自动更正”是全局的,而我只需要对特定文档起作用。

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 前提是您已经定义了样式“ NewStyle”,将SubFnR2中的if语句从

If IsInArray(rng, mykeywords(nKey)) Then

to

If mykeywords(nkey) = LCase(Trim(rng.Text)) Then

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. 顺便说一句,如果您要根据单词的大小写来保留其样式,请删除LCase部分。

Edit: 编辑:

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. 我已经在您给出的示例(剪切并粘贴到word)中对其进行了测试,它改变了两个实例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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM