VBA对Word行、段落和光标位置的移动、选择和操作

!本文可能 超过1年没有更新,今后内容也许不会被维护或者支持,部分内容可能具有时效性,涉及技术细节或者软件使用方面,本人不保证相应的兼容和可操作性。

Mr.Abao’S Blog上看到的,VBA编程时要参考,特地转一下,另外由于嵌入到VBScript脚本中编程,对一些常量我给出了声明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Const wdCharacter = 1
Const wdExtent = 1
Const wdExtend = 1
Const wdGoToBookmark = -1
Const wdLine = 5
Const wdCell = 12
Const wdTableFormatSimple2 = 2
Const wdAlignParagraphRight = 2
Const wdYellow = 7
Const wdToggle = 9999998
Const wdAlignParagraphCenter = 1
Const wdSentence = 3
Const wdParagraph = 4
Const wdStory = 6
Const wdMove = 0

下面我将Mr.Abao’S Blog的内容转帖(略作修改,主要是注释部分增加了VBScript的脚本写法):在WORD中用VBA宏语言选定一行、一段,删除一行、一段,移动光标至行首、行尾、段首、段尾等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Sub MoveToCurrentLineStart()
  ' 移动光标至当前行首
  ' Selection.HomeKey wdLine
  Selection.HomeKey unit:=wdLine
End Sub
Sub MoveToCurrentLineEnd()
  ' 移动光标至当前行尾
  ' Selection.EndKey wdLine
  Selection.EndKey unit:=wdLine
End Sub
Sub SelectToCurrentLineStart()
  ' 选择从光标至当前行首的内容
  ' Selection.HomeKey wdLine, wdExtend
  Selection.HomeKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub SelectToCurrentLineEnd()
  ' 选择从光标至当前行尾的内容
  ' Selection.EndKey wdLine, wdExtend
  Selection.EndKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub SelectCurrentLine()
  ' 选择当前行
  ' Selection.HomeKey wdLine
  ' Selection.EndKey wdLine, wdExtend
  Selection.HomeKey unit:=wdLine
  Selection.EndKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub MoveToDocStart()
  ' 移动光标至文档开始
  ' Selection.HomeKey wdStory
  Selection.HomeKey unit:=wdStory
End Sub
Sub MoveToDocEnd()
  ' 移动光标至文档结尾
  ' Selection.EndKey wdStory
  Selection.EndKey unit:=wdStory
End Sub
Sub SelectToDocStart()
  ' 选择从光标至文档开始的内容
  ' Selection.HomeKey wdStory, wdExtend
  Selection.HomeKey unit:=wdStory, Extend:=wdExtend
End Sub
Sub SelectToDocEnd()
  ' 选择从光标至文档结尾的内容
  ' Selection.EndKey wdStory, wdExtend
  Selection.EndKey unit:=wdStory, Extend:=wdExtend
End Sub
Sub SelectDocAll()
  ' 选择文档全部内容(从WholeStory可猜出Story应是当前文档的意思)
  Selection.WholeStory
End Sub
Sub MoveToCurrentParagraphStart()
  ' 移动光标至当前段落的开始
  ' Selection.MoveUp wdParagraph
  Selection.MoveUp unit:=wdParagraph
End Sub
Sub MoveToCurrentParagraphEnd()
  ' 移动光标至当前段落的结尾
  ' Selection.MoveDown wdParagraph
  Selection.MoveDown unit:=wdParagraph
End Sub
Sub SelectToCurrentParagraphStart()
  ' 选择从光标至当前段落开始的内容
  ' Selection.MoveUp wdParagraph, wdExtend
  Selection.MoveUp unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub SelectToCurrentParagraphEnd()
  ' 选择从光标至当前段落结尾的内容
  ' Selection.MoveDown wdParagraph, wdExtend
  Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub SelectCurrentParagraph()
  ' 选择光标所在段落的内容
  ' Selection.MoveUp wdParagraph
  ' Selection.MoveDown wdParagraph, wdExtend
  Selection.MoveUp unit:=wdParagraph
  Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub DisplaySelectionStartAndEnd()
  '显示选择区的开始与结束的位置,注意:文档第1个字符的位置是0
  MsgBox ("第" & Selection.Start & "个字符至第" & Selection.End & "个字符")
End Sub
Sub DeleteCurrentLine()
  ' 删除当前行
  ' Selection.HomeKey wdLine
  ' Selection.EndKey wdLine, wdExtend
  Selection.HomeKey unit:=wdLine
  Selection.EndKey unit:=wdLine, Extend:=wdExtend
  Selection.Delete
End Sub
Sub DeleteCurrentParagraph()
  ' 删除当前段落
  ' Selection.MoveUp wdParagraph
  ' Selection.MoveDown wdParagraph, wdExtend
  Selection.MoveUp unit:=wdParagraph
  Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
  Selection.Delete
End Sub
若无特别说明,本网站文章均为原创,原则上这些文章不允许转载,但是如果阁下是出于研究学习目的可以转载到阁下的个人博客或者主页,转载遵循创作共同性“署名-非商业性使用-相同方式共享”原则,请转载时注明作者出处谢绝商业性、非署名、采集站、垃圾站或者纯粹为了流量的转载。谢谢合作!
请稍后...

发表评论

电子邮件地址不会被公开。 必填项已用*标注