|
【分享成果,随喜正能量】决定一个人成就的,不是天分,也不是运气,而是坚持和付出,是不停地做,重复的做,用心去做。当你真的努力了,付出了,你会发现自己潜力无限!记得每天鼓励自己,越勤奋,越幸运,越感恩,越幸福!。
我给VBA的定义:VBA是个人小型自动化处理的有效工具。利用好了,可以大大提高自己的劳动效率,而且可以提高数据的准确度。我推出的VBA系列教程共十套(本文的最后附有VBA教程目录和VBA工具目录),现在已经全部完成。
如果您VBA是入门阶段,可以打包选择7.1.3.9教程,第7套教程是入门,第1套教程是入门后的提高,第3套教程字典是必备的VBA之精华,第9套教程是实用的典型案例讲解。如果您有了一定的VBA基础可以根据自己的需要,进行教程的选择。教程提供的程序源码文件就如一座大型的代码库支持着大家的工作。同时还有实用的资料送给学员。
VBA是面向对象编程的语言,博大精深。很多朋友咨询英语和VBA的关系,这套《VBA即用型代码手册(汉英)》集合了众多的案例,案例我用汉语和英语同时发布,学员从中可以更好的领会和掌握VBA中用到的一些英语。今日的内容:WORD_VBA中Selection 对象

第六章 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 &#34;You need to select some text.&#34;
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实践经验,全部浓缩在下面的各个教程中:

【分享成果,随喜正能量】好的人生,都是减法。经历多了才懂得,再多的物质都比不过精神的丰盈。。 |
|