Microsoft Office Word ドキュメントで Range オブジェクトを定義した後、その始点と終点を MoveStart メソッドと MoveEnd メソッドを使用して変更します。 MoveStart と MoveEnd のメソッドは、同じ 2 つの引数 (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)
関連するコンテンツ