你也可以通过使用 Range 对象在 Microsoft Office Word 文档中定义一个范围。 可以通过多种方式选择整个文档,例如,通过使用Select对象的方法Range,或使用类(在文档级自定义项中)或Document类(在 VSTO 外接程序中)的 Content 属性Document。
适用于: 本主题中的信息适用于 Word 的文档级项目和 VSTO 外接程序项目。 有关详细信息,请参阅办公室应用程序和项目类型提供的功能。
定义范围
下面的示例演示如何创建一个新的 Range 对象,该对象包括活动文档中的前七个字符,其中包括非打印字符。 然后,它选择范围内的文本。
在文档级自定义项中定义范围
通过将开始和结束字符传递到 Document 类的 Range 方法来将范围添加到文档中。 若要使用此代码示例,请从项目中的 ThisDocument 类运行它。
if (this.Sentences.Count >= 2)
{
object startLocation = this.Sentences[2].Start;
object endLocation = this.Sentences[2].End;
// Supply a Start and End value for the Range.
rng = this.Range(ref startLocation, ref endLocation);
// Select the Range.
rng.Select();
}
If Me.Sentences.Count >= 2 Then
Dim startLocation As Object = Me.Sentences(2).Start
Dim endLocation As Object = Me.Sentences(2).End
' Supply a Start and End value for the Range.
rng = Me.Range(Start:=startLocation, End:=endLocation)
' Select the Range
rng.Select()
End If
Word.Document document = this.Application.ActiveDocument;
if (document.Sentences.Count >= 2)
{
object startLocation = document.Sentences[2].Start;
object endLocation = document.Sentences[2].End;
// Supply a Start and End value for the Range.
rng = document.Range(ref startLocation, ref endLocation);
// Select the Range.
rng.Select();
}
Dim document As Word.Document = Me.Application.ActiveDocument
If document.Sentences.Count >= 2 Then
Dim startLocation As Object = document.Sentences(2).Start
Dim endLocation As Object = document.Sentences(2).End
' Supply a Start and End value for the Range.
rng = document.Range(Start:=startLocation, End:=endLocation)
' Select the Range
rng.Select()
End If