定义 Microsoft Office Word 文档中的 Range 对象后,可以通过使用 MoveStart 和 MoveEnd 方法来更改其起点和终点。 和MoveStartMoveEnd方法采用相同的两个参数:Unit 和 Count。 Count 参数是要移动的单位数,Unit 参数可以是以下WdUnits值之一:
若要扩展范围
定义字符的范围。 有关详细信息,请参阅 “如何:以编程方式定义和选择文档中的范围”。
下面的代码示例可用于文档级自定义项。
object start = 0;
object end = 7;
Word.Range rng = this.Range(ref start, ref end);
Dim rng As Word.Range = Me.Range(Start:=0, End:=7)
以下代码示例可用于 VSTO 外接程序。 本示例使用活动文档。
Word.Range rng = this.Application.ActiveDocument.Range(0, 7);
Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=7)
使用 MoveStart 对象的 Range 方法移动范围的开始位置。
rng.MoveStart(Word.WdUnits.wdCharacter, 7);
rng.MoveStart(Unit:=Word.WdUnits.wdCharacter, Count:=7)
使用 MoveEnd 对象的 Range 方法移动范围的结束位置。
rng.MoveEnd(Word.WdUnits.wdCharacter, 7);
rng.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=7)
文档级自定义代码
若要在文档级自定义项中扩展范围
下面的示例显示文档级自定项的完整代码。 若要使用此代码,请从项目中的 ThisDocument
类运行它。
// Define a range of 7 characters.
object start = 0;
object end = 7;
Word.Range rng = this.Range(ref start, ref end);
// Move the start position 7 characters.
rng.MoveStart(Word.WdUnits.wdCharacter, 7);
// Move the end position 7 characters.
rng.MoveEnd(Word.WdUnits.wdCharacter, 7);
' Define a range of 7 characters.
Dim rng As Word.Range = Me.Range(Start:=0, End:=7)
' Move the start position 7 characters.
rng.MoveStart(Unit:=Word.WdUnits.wdCharacter, Count:=7)
' Move the end position 7 characters.
rng.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=7)
VSTO 外接程序代码
若要扩展应用程序级 VSTO 外接程序中的范围
下面的示例显示 VSTO 外接程序的完整代码。 若要使用此代码,请从项目中的 ThisAddIn
类运行它。
// Define a range of 7 characters.
Word.Range rng = this.Application.ActiveDocument.Range(0, 7);
// Move the start position 7 characters.
rng.MoveStart(Word.WdUnits.wdCharacter, 7);
// Move the end position 7 characters.
rng.MoveEnd(Word.WdUnits.wdCharacter, 7);
' Define a range of 7 characters.
Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=7)
' Move the start position 7 characters.
rng.MoveStart(Unit:=Word.WdUnits.wdCharacter, Count:=7)
' Move the end position 7 characters.
rng.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=7)
相关内容