你也可以通过使用 Range 对象在 Microsoft Office Word 文档中定义一个范围。 可以通过多种方式选择整个文档,例如,通过使用Select对象的方法Range,或使用类(在文档级自定义项中)或Document类(在 VSTO 外接程序中)的 Content 属性Document。
适用于: 本主题中的信息适用于 Word 的文档级项目和 VSTO 外接程序项目。 有关详细信息,请参阅办公室应用程序和项目类型提供的功能。
定义范围
下面的示例演示如何创建一个新的 Range 对象,该对象包括活动文档中的前七个字符,其中包括非打印字符。 然后,它选择范围内的文本。
在文档级自定义项中定义范围
通过将开始和结束字符传递到 Document 类的 Range 方法来将范围添加到文档中。 若要使用此代码示例,请从项目中的 ThisDocument
类运行它。
object start = 0;
object end = 7;
Word.Range rng = this.Range(ref start, ref end);
rng.Select();
Dim rng As Word.Range = Me.Range(Start:=0, End:=7)
rng.Select()
通过使用 VSTO 外接程序定义范围
通过将开始和结束字符传递到 Document 类的 Range 方法来将范围添加到文档中。 下面的代码示例将一个范围添加到活动文档。 若要使用此代码示例,请从项目中的 ThisAddIn
类运行它。
Word.Range rng = this.Application.ActiveDocument.Range(0, 7);
rng.Select();
Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=7)
rng.Select()
在文档级自定义中选择范围
下面的示例演示如何通过使用 Range 对象的 Select 方法,或者通过使用 Document 类的 Content 属性来选择整个文档。
通过使用 Select 方法来选择整个文档作为范围
使用包含整个文档的 Range 的 Select 方法。 若要使用下面的代码示例,请从项目的 ThisDocument
类中运行它。
object start = this.Content.Start;
object end = this.Content.End;
this.Range(ref start, ref end).Select();
通过使用 Content 属性来选择整个文档作为范围
使用 Content 属性定义包含整个文档的范围。
你还可以使用其他对象的方法和属性来定义范围。
在活动文档中选择一个句子
通过使用 Sentences 集合设置范围。 使用你要选择的句子的索引。
Word.Range s2 = this.Sentences[2];
s2.Select();
Dim s2 As Word.Range = Me.Sentences(2)
s2.Select()
选择句子的另一种方法是手动设置范围的开始和结束值。
通过手动设置开始和结束值来选择一个句子
创建一个范围变量。
检查文档中是否至少有两个句子,设置 范围的 Start 和 End 参数,然后选择该区域。
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
使用 VSTO 外接程序选择范围
下面的示例演示如何通过使用 Range 对象的 Select 方法,或者通过使用 Document 类的 Content 属性来选择整个文档。
通过使用 Select 方法来选择整个文档作为范围
使用包含整个文档的 Range 的 Select 方法。 下面的代码示例选择活动文档的内容。 若要使用此代码示例,请从项目中的 ThisAddIn
类运行它。
this.Application.ActiveDocument.Range(
this.Application.ActiveDocument.Content.Start,
this.Application.ActiveDocument.Content.End).Select();
Me.Application.ActiveDocument.Range.Select()
通过使用 Content 属性来选择整个文档作为范围
使用 Content 属性定义包含整个文档的范围。
this.Application.ActiveDocument.Content.Select();
Me.Application.ActiveDocument.Content.Select()
你还可以使用其他对象的方法和属性来定义范围。
在活动文档中选择一个句子
通过使用 Sentences 集合设置范围。 使用你要选择的句子的索引。
Word.Range s2 = this.Application.ActiveDocument.Sentences[2];
s2.Select();
Dim s2 As Word.Range = Me.Application.ActiveDocument.Sentences(2)
s2.Select()
选择句子的另一种方法是手动设置范围的开始和结束值。
通过手动设置开始和结束值来选择一个句子
创建一个范围变量。
检查文档中是否至少有两个句子,设置 范围的 Start 和 End 参数,然后选择该区域。
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
相关内容