我给VBA下的定义:VBA是个人小型自动化处理的有效工具。可以大大提高自己的劳动效率,而且可以提高数据的准确性。我这里专注VBA,将我多年的经验汇集在VBA系列九套教程中。
作为我的学员要利用我的积木编程思想,积木编程最重要的是积木如何搭建及拥有积木。在九套教程中我给出了大量的积木,同时讲解了如何搭建。为了让学员拥有更多的积木,我开始着手这部《VBA即用型代码手册(汉英)》的创作,这部手册约600页,集合约500多个的案例,案例我用汉语和英语同时发布,一方面学员从中可以更好的领会和掌握VBA中用到的一些英语知识,另一方面,大家可以看到各种各样的积木。这部手册是大家学习和工作中的不可多得的实用资料。今日的内容是: VBA即用型代码手册:Selection 对象 Selection Object
【分享成果,随喜正能量】200 心好命又好,富贵直到老;命好心不好,福变为祸兆;心好命不好,祸转为福报;心命俱不好,遭殃且贫夭;心可挽乎命,最要存仁道;命实造于心,吉凶惟人召;信命不修心,阴阳恐虚矫;修心一听命,天地自相保。。
第六章 Word对象及示例
Word Objects and Macro Examples
4 Selection 对象 Selection Object
Selection对象 - 选定的范围或光标位置。
Selection Object – A selected range or cursor location.
1)选择活动文档中的第二段:
select the second paragraph in active document:
ActiveDocument.Paragraphs(2).Range.Select
2)使用选择对象键入一些文本:
you can use the Selection Object to type some text:
Selection.TypeText "Some text"
3)我们可以在“Some text”下面输入一些段落:
We can type some paragraphs bellow “Some text”:
Selection.TypeText "Some text"
Selection.TypeParagraph
4)通常,我们有必要知道是否选择了某些文本,或者只有一个插入点:
Often, it’s necessary to know if some text is selected or we have just a insertion point:
If Selection.Type <> wdSelectionIP Then
Selection.Font.Bold = True
Else
MsgBox "You need to select some text."
End If
5)文件开头
Beginning of document:
Selection.HomeKey Unit:=wdStory, Extend:=wdMove
6)当前行的开头:
Beginning of current line:
Selection.HomeKey Unit:=wdLine, Extend:=wdMove
7)Extend 参数可以移动插入点。您可以使用 wdExtend 来选择当前插入点之间的所有文本。
The Extend parameter wdMove moves the insertion point. Instead, you could use wdExtend which will select all text between the current insertion point.
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
8) 更改插入点位置最有用的方法是移动。 向前移动选择两个字符:
The most useful method for changing position of insertion point is Move. To move Selection two characters forward:
Selection.Move Unit:=wdCharacter, Count:=2
9) 要将其向后移动,请对 Count 参数使用负数:
to move it backwards, use negative number for Count parameter:
Selection.Move Unit:=wdCharacter, Count:=-2
10) 改为移动单词:
To move words instead:
Selection.Move unit:=wdWord, Count:=2
Selection更容易使用(与range相比),因为它就像使用 Word 的机器人,模仿人类用户。 插入点在哪里——会发生一些动作。 但是,这意味着您必须注意插入点的位置!在代码中执行许多步骤后,这并不容易。 否则,Word 会在不需要的地方更改文本。
如果您需要选择对象中不可用的某些属性或方法,您总是可以轻松获得与选择相关的范围:
Selection is easier to work with (compared to ranges) because it is like a robot using Word, mimicking human user. Where Insertion point is – some action would take place. But, this means that you must take care where insertion point is! This is not easy after many steps in code. Otherwise, Word would change text in not desired place.
In the case you need some property or method not available in Selection object you can always easily obtain range associated with selection:
Set oRange = Selection.Range
提示:使用选择通常比使用范围更容易,但也更慢(在处理大文档时很重要)
TIP: Using Selection is often easier than using ranges, but also it’s way slower (important when you deal with big documents)
我20多年的VBA实践经验,全部浓缩在下面的各个教程中: