在WORD中用VBA實現游標移動與內容選擇

來源:互聯網
上載者:User
在WORD中如何用VBA宏語言選定一行、一段,刪除一行、一段,移動游標至行首、行尾、段首、段尾等。請看以下內容。Sub MoveToCurrentLineStart()
    '移動游標至當前行首
    Selection.HomeKey unit:=wdLine
End SubSub MoveToCurrentLineEnd()
    '移動游標至當前行尾
    Selection.EndKey unit:=wdLine
End SubSub SelectToCurrentLineStart()
    '選擇從游標至當前行首的內容
    Selection.HomeKey unit:=wdLine, Extend:=wdExtend
End SubSub SelectToCurrentLineEnd()
    '選擇從游標至當前行尾的內容
    Selection.EndKey unit:=wdLine, Extend:=wdExtend
End SubSub SelectCurrentLine()
    '選擇當前行
    Selection.HomeKey unit:=wdLine
    Selection.EndKey unit:=wdLine, Extend:=wdExtend
End SubSub MoveToDocStart()
    '移動游標至文檔開始
    Selection.HomeKey unit:=wdStory
End SubSub MoveToDocEnd()
    '移動游標至文檔結尾
    Selection.EndKey unit:=wdStory
End SubSub SelectToDocStart()
    '選擇從游標至文檔開始的內容
    Selection.HomeKey unit:=wdStory, Extend:=wdExtend
End SubSub SelectToDocEnd()
    '選擇從游標至文檔結尾的內容
    Selection.EndKey unit:=wdStory, Extend:=wdExtend
End SubSub SelectDocAll()
    '選擇文檔全部內容(從WholeStory可猜出Story應是當前文檔的意思)
    Selection.WholeStory
End SubSub MoveToCurrentParagraphStart()
    '移動游標至當前段落的開始
    Selection.MoveUp unit:=wdParagraph
End SubSub MoveToCurrentParagraphEnd()
    '移動游標至當前段落的結尾
    Selection.MoveDown unit:=wdParagraph
End SubSub SelectToCurrentParagraphStart()
    '選擇從游標至當前段落開始的內容
    Selection.MoveUp unit:=wdParagraph, Extend:=wdExtend
End SubSub SelectToCurrentParagraphEnd()
    '選擇從游標至當前段落結尾的內容
    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
End SubSub SelectCurrentParagraph()
    '選擇游標所在段落的內容
    Selection.MoveUp unit:=wdParagraph
    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
End SubSub DisplaySelectionStartAndEnd()
    '顯示選擇區的開始與結束的位置,注意:文檔第1個字元的位置是0
    MsgBox ("第" & Selection.Start & "個字元至第" & Selection.End & "個字元")
End SubSub DeleteCurrentLine()
    '刪除當前行
    Selection.HomeKey unit:=wdLine
    Selection.EndKey unit:=wdLine, Extend:=wdExtend
    Selection.Delete
End SubSub DeleteCurrentParagraph()
    '刪除當前段落
    Selection.MoveUp unit:=wdParagraph
    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
    Selection.Delete
End Sub

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.