从当前光标位置创建范围 Word 2010 VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16807860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:19:12  来源:igfitidea点击:

Create Range from current cursor position Word 2010 VBA

vbaword-vbaword-2010

提问by Silviu Preda

I have a .docm document with a user form. On the user form I have a button that needs to insert something (some text, for starter) at the last known cursor position in the document. For this, I want to create a range.

我有一个带有用户表单的 .docm 文档。在用户表单上,我有一个按钮需要在文档中最后一个已知的光标位置插入一些东西(一些文本,对于初学者)。为此,我想创建一个范围。

How can I get the starting position for this range?

我怎样才能获得这个范围的起始位置?

回答by Olle Sj?gren

  • Selectionis the current position in the document.
  • Selection.Rangeis the Range object of the current Selection.
  • Range.Startis the start position of a Rangeobject (returns or sets a Long).
  • Selection是文档中的当前位置。
  • Selection.Range是当前 的 Range 对象Selection
  • Range.StartRange对象的开始位置(返回或设置一个 Long)。

If you combine those three you get the cursor position (or the start of the selection if you have text selected):

如果您将这三个组合在一起,您将获得光标位置(如果您选择了文本,则为选择的开始):

Selection.Range.Start

If you only want to enter text att the cursor position the following will do:

如果您只想在光标位置输入文本,请执行以下操作:

Selection.TypeText "Test"