次の方法で共有


方法: プログラムによって文書内のテキストに書式を設定する

Range オブジェクトを使用することにより、Microsoft Office Word 文書のテキストの書式を設定できます。

対象: このトピックの情報は、Word 2013 と Word 2010 のドキュメント レベルのプロジェクトおよびアプリケーション レベルのプロジェクトに適用されます。詳細については、「Office アプリケーションおよびプロジェクト タイプ別の使用可能な機能」を参照してください。

次の例では、文書内の最初の段落を選択し、フォント サイズ、フォント名、および配置を変更します。次に、範囲を選択し、コードの次のセクションを実行する前に一時停止するためのメッセージ ボックスを表示します。次のセクションでは、Microsoft.Office.Tools.Word.Document ホスト項目 (ドキュメント レベルのカスタマイズの場合) または Microsoft.Office.Interop.Word.Document クラス (アプリケーション レベルのアドインの場合) の Undo メソッドを呼び出します。標準のインデント スタイルを適用し、メッセージ ボックスを表示してコードを一時停止します。最後に、Undo メソッドを 1 回呼び出し、メッセージ ボックスを表示します。

ドキュメント レベルのカスタマイズの例

ドキュメント レベルのカスタマイズを使用してテキストの書式を設定するには

  • 次のコード例はドキュメント レベルのカスタマイズで使用できます。このコードを使用するには、プロジェクトの ThisDocument クラスから実行します。

    Private Sub RangeFormat()
    
        ' Set the Range to the first paragraph.
        Dim rng As Word.Range = Me.Paragraphs(1).Range
    
        ' Change the formatting. To change the font size for a right-to-left language, 
        ' such as Arabic or Hebrew, use the Font.SizeBi property instead of Font.Size.
        rng.Font.Size = 14
        rng.Font.Name = "Arial"
        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
    
        rng.Select()
        MessageBox.Show("Formatted Range")
    
        ' Undo the three previous actions.
        Me.Undo(Times:=3)
    
        rng.Select()
        MessageBox.Show("Undo 3 actions")
    
        ' Apply the Normal Indent style.
        rng.Style = "Normal Indent"
    
        rng.Select()
        MessageBox.Show("Normal Indent style applied")
    
        ' Undo a single action.
        Me.Undo()
    
        rng.Select()
        MessageBox.Show("Undo 1 action")
    End Sub
    
    private void RangeFormat() 
    { 
        // Set the Range to the first paragraph. 
        Word.Range rng = this.Paragraphs[1].Range;
    
        // Change the formatting. To change the font size for a right-to-left language, 
        // such as Arabic or Hebrew, use the Font.SizeBi property instead of Font.Size.
        rng.Font.Size = 14; 
        rng.Font.Name = "Arial"; 
        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    
        rng.Select(); 
        MessageBox.Show("Formatted Range"); 
    
        // Undo the three previous actions. 
        object numTimes3 = 3; 
        this.Undo(ref numTimes3); 
    
        rng.Select(); 
        MessageBox.Show("Undo 3 actions"); 
    
        // Apply the Normal Indent style. 
        object indentStyle = "Normal Indent"; 
        rng.set_Style(ref indentStyle); 
    
        rng.Select(); 
        MessageBox.Show("Normal Indent style applied"); 
    
        // Undo a single action. 
        object numTimes1 = 1; 
        this.Undo(ref numTimes1); 
    
        rng.Select(); 
        MessageBox.Show("Undo 1 action"); 
    }
    

アプリケーション レベルのアドインの例

アプリケーション レベルのアドインを使用してテキストの書式を設定するには

  • 次のコード例はアプリケーション レベルのアドインで使用できます。この例ではアクティブ ドキュメントを使用します。このコードを使用するには、プロジェクトの ThisAddIn クラスから実行します。

    Private Sub RangeFormat()
    
        ' Set the Range to the first paragraph.
        Dim document As Word.Document = Me.Application.ActiveDocument
        Dim rng As Word.Range = document.Paragraphs(1).Range
    
        ' Change the formatting. To change the font size for a right-to-left language, 
        ' such as Arabic or Hebrew, use the Font.SizeBi property instead of Font.Size.
        rng.Font.Size = 14
        rng.Font.Name = "Arial"
        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
    
        rng.Select()
        MessageBox.Show("Formatted Range")
    
        ' Undo the three previous actions.
        document.Undo(Times:=3)
    
        rng.Select()
        MessageBox.Show("Undo 3 actions")
    
        ' Apply the Normal Indent style.
        rng.Style = "Normal Indent"
    
        rng.Select()
        MessageBox.Show("Normal Indent style applied")
    
        ' Undo a single action.
        document.Undo()
    
        rng.Select()
        MessageBox.Show("Undo 1 action")
    End Sub
    
    private void RangeFormat()
    {
        // Set the Range to the first paragraph. 
        Word.Document document = this.Application.ActiveDocument;
        Word.Range rng = document.Paragraphs[1].Range;
    
        // Change the formatting. To change the font size for a right-to-left language, 
        // such as Arabic or Hebrew, use the Font.SizeBi property instead of Font.Size.
        rng.Font.Size = 14;
        rng.Font.Name = "Arial";
        rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
    
        rng.Select();
        MessageBox.Show("Formatted Range");
    
        // Undo the three previous actions. 
        object numTimes3 = 3;
        document.Undo(ref numTimes3);
    
        rng.Select();
        MessageBox.Show("Undo 3 actions");
    
        // Apply the Normal Indent style. 
        object indentStyle = "Normal Indent";
        rng.set_Style(ref indentStyle);
    
        rng.Select();
        MessageBox.Show("Normal Indent style applied");
    
        // Undo a single action. 
        object numTimes1 = 1;
        document.Undo(ref numTimes1);
    
        rng.Select();
        MessageBox.Show("Undo 1 action");
    }
    

参照

処理手順

方法: プログラムによって文書に複数の範囲を定義して選択する

方法: プログラムによって Word 文書にテキストを挿入する

方法: プログラムによって文書内のテキストを検索および置換する

関連項目

SizeBi