word vba 控制光标常用代码

  1. Selection.MoveDown Unit:=wdLine, Count:=1'光标下移一行
  2. ’选中光标所在行
  3. With Selection
  4.         .HomeKey Unit:=wdLine, Extend:=wdExtend
  5.         .MoveEnd Unit:=wdLine, Count:=1
  6.   End With
  1. 移动光标至文档开始
  2. Selection.HomeKey unit:=wdStory
  3. 下面的供参考:
  4. Sub MoveToCurrentLineStart()
  5. '移动光标至当前行首
  6. Selection.HomeKey unit:=wdLine
  7. End Sub
  8. Sub MoveToCurrentLineEnd()
  9. '移动光标至当前行尾
  10. Selection.EndKey unit:=wdLine
  11. End Sub
  12. Sub SelectToCurrentLineStart()
  13. '选择从光标至当前行首的内容
  14. Selection.HomeKey unit:=wdLine, Extend:=wdExtend
  15. End Sub
  16. Sub SelectToCurrentLineEnd()
  17. '选择从光标至当前行尾的内容
  18. Selection.EndKey unit:=wdLine, Extend:=wdExtend
  19. End Sub
  20. Sub SelectCurrentLine()
  21. '选择当前行
  22. Selection.HomeKey unit:=wdLine
  23. Selection.EndKey unit:=wdLine, Extend:=wdExtend
  24. End Sub
  25. Sub MoveToDocStart()
  26. '移动光标至文档开始
  27. Selection.HomeKey unit:=wdStory
  28. End Sub
  29. Sub MoveToDocEnd()
  30. '移动光标至文档结尾
  31. Selection.EndKey unit:=wdStory
  32. End Sub
  33. Sub SelectToDocStart()
  34. '选择从光标至文档开始的内容
  35. Selection.HomeKey unit:=wdStory, Extend:=wdExtend
  36. End Sub
  37. Sub SelectToDocEnd()
  38. '选择从光标至文档结尾的内容
  39. Selection.EndKey unit:=wdStory, Extend:=wdExtend
  40. End Sub
  41. Sub SelectDocAll()
  42. '选择文档全部内容(从WholeStory可猜出Story应是当前文档的意思)
  43. Selection.WholeStory
  44. End Sub
  45. Sub MoveToCurrentParagraphStart()
  46. '移动光标至当前段落的开始
  47. Selection.MoveUp unit:=wdParagraph
  48. End Sub
  49. Sub MoveToCurrentParagraphEnd()
  50. '移动光标至当前段落的结尾
  51. Selection.MoveDown unit:=wdParagraph
  52. End Sub
  53. Sub SelectToCurrentParagraphStart()
  54. '选择从光标至当前段落开始的内容
  55. Selection.MoveUp unit:=wdParagraph, Extend:=wdExtend
  56. End Sub
  57. Sub SelectToCurrentParagraphEnd()
  58. '选择从光标至当前段落结尾的内容
  59. Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
  60. End Sub
  61. Sub SelectCurrentParagraph()
  62. '选择光标所在段落的内容
  63. Selection.MoveUp unit:=wdParagraph
  64. Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
  65. End Sub
  66. Sub DisplaySelectionStartAndEnd()
  67. '显示选择区的开始与结束的位置,注意:文档第1个字符的位置是0
  68. MsgBox ("第" & Selection.Start & "个字符至第" & Selection.End & "个字符")
  69. End Sub
  70. Sub DeleteCurrentLine()
  71. '删除当前行
  72. Selection.HomeKey unit:=wdLine
  73. Selection.EndKey unit:=wdLine, Extend:=wdExtend
  74. Selection.Delete
  75. End Sub
  76. Sub DeleteCurrentParagraph()
  77. '删除当前段落
  78. Selection.MoveUp unit:=wdParagraph
  79. Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
  80. Selection.Delete
  81. End Sub
(0)

相关推荐