Word开发之VBA操作行、段落、光标位置的代码

Admin | 2010-3-6 21:45:19 | TrackRecord: 2181 Times | Tag标签:Word教程 打印本页

您当前所处的位置是:〖首页〗→【文章页】 本站共有16个图文教程栏目,请用心拜读!

本站提供经典的Excel公式函数实例,Word排版技巧,PPT教程;同时更兼有Flash,PowerPoint,数据库等技术文章。

   在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()

  '选择文档全部内容
  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



上一篇:给Word文档快速另起一页(翻页)的最快    下一篇:word无法使用鼠标左键拖动选择文字

会员评论列表:
针对本篇文章或本站,请您发表个人的建议或批评!
FreeBoxPc

谷歌搜索 百度搜索 本站仅与内容具备一定的实用价值的原创网站交换友情链接,力争为大众做出更优质的服务!
All Rights Reserved版权所有 本站备案信息:滇ICP备11001339号-2 站长联系方式 Email:dzwebs@126.com