HOW TO:在文件中定義及選取範圍
您可以使用 Range 物件來定義 Microsoft Office Word 文件中的範圍。 您有多種方式可以選取整個文件,例如,使用 Range 物件的 Select 方法,或是使用 Microsoft.Office.Tools.Word.Document 類別 (Class) (如果是在文件層級自訂中) 或 Microsoft.Office.Interop.Word.Document 類別 (如果是在應用程式層級增益集中) 的 Content 屬性。
**適用於:**本主題中的資訊適用於 Word 2007 和 Word 2010 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
定義範圍
下列範例示範如何建立新的 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()
Word.Range rng = this.Application.ActiveDocument.Range(0, 7); 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()
this.Application.ActiveDocument.Range( this.Application.ActiveDocument.Content.Start, this.Application.ActiveDocument.Content.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(); }