wdtitleword - 使用 vba 应用于多个单词 [英] wdtitleword - use vba to apply to more than one word
问题描述
在 Word 中,在文档用户按住 Ctrl 键选择一堆字符串、单词、句子等之后,可以轻松地使用 vba 将突出显示应用于多个字符串、单词、句子等.但是,当我 Ctrl-select 几个单词并运行以下语句时,只有我选择的最后一个单词被更改.有没有办法将 wdtitleword 应用于多个选定的单词?谢谢.
In Word it's easy to use vba to apply a highlight to more than one string, word, sentence etc after the user of the document has Ctrl-selected a bunch of them. However, when I Ctrl-select a few words and run the following statement, only the last of my selected words is changed. Is there a way to apply wdtitleword to more than one selected word? Thanks.
sub a()
Selection.Range.Case = wdTitleWord
end sub
推荐答案
请试试这个:
Sub changeNonContigCase()
' Find the non-contig selection
If Selection.Font.Shading.BackgroundPatternColor = wdColorAutomatic Then
Selection.Font.Shading.BackgroundPatternColor = whtcolor
End If
' Find and process each range with .Font.Shading.BackgroundPatternColor = WhtColor
ActiveDocument.Range.Select
Selection.Collapse wdCollapseStart
With Selection.Find
.Font.Shading.BackgroundPatternColor = whtcolor
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
' Do what you need
Selection.Range.Case = wdTitleWord
' Reset shading as you go
Selection.Font.Shading.BackgroundPatternColor = wdColorAutomatic
' Setup to find the next selection
Selection.Collapse wdCollapseEnd
Loop
End With
End Sub
这有效,但是间接的.我认为没有更直接的方法来实现这一目标.您可以修改以避免重置需要保留的现有格式.直到现在我什至不知道可以在 MS Word 中选择一个不连续的范围,可惜在 VBA 中使用它并不容易.
This works, but is indirect. I don't think there is a more direct way to achieve this. You can modify to avoid resetting existing formatting that you need to preserve. Until now I didn't even know that it was possible to select a non-contiguous range in MS Word, pity it is not easier to work with in VBA.
这篇关于wdtitleword - 使用 vba 应用于多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!