在WORD中用VBA实现光标移动与内容选择

在WORD中如何用VBA宏语言选定一行、一段,删除一行、一段,移动光标至行首、行尾、段首、段尾等。请看以下内容。
Sub MoveToCurrentLineStart()
    '移动光标至当前行首
    Selection.HomeKey unit:=wdLine
End Sub
Sub MoveToCurrentLineEnd()
    '移动光标至当前行尾
    Selection.EndKey unit:=wdLine
End Sub
Sub SelectToCurrentLineStart()
    '选择从光标至当前行首的内容
    Selection.HomeKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub SelectToCurrentLineEnd()
    '选择从光标至当前行尾的内容
    Selection.EndKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub SelectCurrentLine()
    '选择当前行
    Selection.HomeKey unit:=wdLine
    Selection.EndKey unit:=wdLine, Extend:=wdExtend
End Sub
Sub MoveToDocStart()
    '移动光标至文档开始
    Selection.HomeKey unit:=wdStory
End Sub
Sub MoveToDocEnd()
    '移动光标至文档结尾
    Selection.EndKey unit:=wdStory
End Sub
Sub SelectToDocStart()
    '选择从光标至文档开始的内容
    Selection.HomeKey unit:=wdStory, Extend:=wdExtend
End Sub
Sub SelectToDocEnd()
    '选择从光标至文档结尾的内容
    Selection.EndKey unit:=wdStory, Extend:=wdExtend
End Sub
Sub SelectDocAll()
    '选择文档全部内容(从WholeStory可猜出Story应是当前文档的意思)
    Selection.WholeStory
End Sub
Sub MoveToCurrentParagraphStart()
    '移动光标至当前段落的开始
    Selection.MoveUp unit:=wdParagraph
End Sub
Sub MoveToCurrentParagraphEnd()
    '移动光标至当前段落的结尾
    Selection.MoveDown unit:=wdParagraph
End Sub
Sub SelectToCurrentParagraphStart()
    '选择从光标至当前段落开始的内容
    Selection.MoveUp unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub SelectToCurrentParagraphEnd()
    '选择从光标至当前段落结尾的内容
    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
End Sub
Sub SelectCurrentParagraph()
    '选择光标所在段落的内容
    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 unit:=wdLine
    Selection.EndKey unit:=wdLine, Extend:=wdExtend
    Selection.Delete
End Sub
Sub DeleteCurrentParagraph()
    '删除当前段落
    Selection.MoveUp unit:=wdParagraph
    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
    Selection.Delete
End Sub

Sub 宏2()

'

' 宏2

'

'

    Selection.MoveUp unit:=wdParagraph

    Selection.InsertBefore Text:="new text"

    Selection.InsertAfter Text:="The end"

    Selection.MoveLeft unit:=wdCharacter, Count:=1

End Sub

 


Sub 宏4()

'

宏4 

'

'

Set myRange = ActiveDocument.Content

  myRange.Find.Execute FindText:=",", ReplaceWith:=",^p^t", Replace:=wdReplaceAll

 

End Sub

 

宏F12、在?。后加入换行

 

宏F11、将    ,:     :,  :。    。:   ?:  :?  都替换成:“

 

宏F10、替换正确的关键词,领域时——领域石,小孩——小黑

 

宏F9、将数字都替换为中文

 

宏F1、到行首,加入“

 

宏F2、到行尾,加入”

 

宏F3、到行尾

 

宏F4、到行尾,Selection.Delete Unit:=wdCharacter, Count:=2       删除2个换行

 

宏F5、插入“

 

宏F6、插入”

 

 

开始

a=selection.start

 

结尾

b=selection.end

 

Selection.InsertBefore Text:="new text"

Selection.InsertAfter Text:="The end."

Sub MoveToCurrentParagraphStart()

    '移动光标至当前段落的开始

    Selection.MoveUp unit:=wdParagraph

End Sub

Sub MoveToCurrentParagraphEnd()

    '移动光标至当前段落的结尾

    Selection.MoveDown unit:=wdParagraph

End Sub

 

Selection.MoveUp unit:=wdParagraph

Selection.InsertBefore Text:="new text "

Selection.MoveDown Unit:=wdParagraph

Selection.MoveLeft Unit:=wdCharacter, Count:=1

Selection.InsertAfter Text:="The end."

 

    Selection.Delete Unit:=wdCharacter, Count:=1

    Selection.TypeBackspace

 







 

posted @ 2019-06-28 19:39  预估计  阅读(4443)  评论(0编辑  收藏  举报