在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

 







 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM