循环浏览MS * Word * VBA中非连续选择的各个部分 [英] Looping through the parts of a non-contiguous selection in MS *Word* VBA

查看:388
本文介绍了循环浏览MS * Word * VBA中非连续选择的各个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MS Word 2013 VBA中有一个宏( 不是 Excel),该宏可以切换所选文本的突出显示颜色.代码看起来像 这个:

I have a macro in MS Word 2013 VBA (not Excel) that toggles the selected text's highlight color. Code looks like this:

If Selection.Range.HighlightColorIndex = WhtColor Then Selection.Range.HighlightColorIndex = wdNoHighlight Else Selection.Range.HighlightColorIndex = WhtColor

这对于连续/连续选择非常有用.但是,如果我 选择一个Word表中的4个非连续行(例如,第5、12行) 15和19),该宏仅突出显示所选的最后一行.

That works great for continuous/contiguous selections. But, if I select, say, 4 non-contiguous rows in a Word table (say, rows 5, 12, 15, and 19), the macro highlights only the last row selected.

如何获取HighlightColorIndex应用于广告的所有部分" 非连续范围,或者如何遍历不同的部分" 范围,然后将HighlightColorIndex应用于每个部分?

How do I get the HighlightColorIndex to apply to all "parts" of the noncontiguous range, or, how do I loop through the different "parts" of the range and apply the HighlightColorIndex to each part?

推荐答案

蒂姆·威廉姆斯(Tim Williams)指向的网页( support.microsoft.com/en-us/kb/288424 )提供了有关如何实现此目标的线索.但是,该链接确实表明,不能通过非连续选择进行循环.

The web page that Tim Williams points to (support.microsoft.com/en-us/kb/288424) gives a clue about how this is possible. But, that link does show that one cannot loop through a non-contiguous selection.

尽管如此,该链接还显示的是可以为非连续选择设置字体格式 ,但不能为范围对象设置字体格式.

Nevertheless, what that link also shows is that Font formatting can be set for a non-contiguous selection, but not for a range object.

这是用于非连续选择的修订代码:

Here is the revised code that does work for a non-contiguous selection:

If Selection.Font.Shading.BackgroundPatternColor = WhtColor Then Selection.Font.Shading.BackgroundPatternColor = wdColorAutomatic Else Selection.Font.Shading.BackgroundPatternColor = WhtColor

该代码会将背景颜色更改为选定的目标颜色(尽管我不得不将颜色代码从Wd常量更改为WdColor常量).

That code will change the background color to the target color selected (although I had to change the color codes from Wd constants to WdColor constants).

此方法的唯一缺点是,我不知道一种搜索背景颜色已更改的文本的方法,而您可以搜索突出显示的文本.

The only drawback with this approach is that I do not know a way to search for text whose background color has been changed, whereas you can search for text that is highlighted.

无论如何,谢谢@Tim Williams的有用链接.希望以上内容对其他只想更改字体属性并且实际上不必遍历所选范围的各个部分的人有所帮助.

Anyway, thanks, @Tim Williams for the helpful link. Hope the above helps someone else who just wants to change font properties and does not actually have to loop through the separate pieces of the selected range.

这篇关于循环浏览MS * Word * VBA中非连续选择的各个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆