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