如何:在文档中设置文本格式

更新:2007 年 11 月

适用对象

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 文档级项目

  • 应用程序级项目

Microsoft Office 版本

  • Word 2003

  • Word 2007

有关更多信息,请参见按应用程序和项目类型提供的功能

可以使用 Range 对象在 Microsoft Office Word 文档中设置文本格式。

下面的示例选择文档的第一段并更改字号、字体名称和对齐方式。然后,它将选择范围并显示一个消息框,以在执行代码的下一节之前暂停。下一节将调用 Microsoft.Office.Tools.Word.Document 宿主项(对于文档级自定义项)或 Microsoft.Office.Interop.Word.Document 类(对于应用程序级外接程序)的 Undo 方法三次。该示例应用“正常缩进”样式,并显示一个消息框以暂停代码。然后代码调用一次 Undo 方法,并显示一个消息框。

说明:

您会发现,如果为所需的所有格式设置都定义了样式,则可以减少编写格式设置代码的工作量。并且,样式的维护也更加容易。如果需要更改文档的格式设置,只需在一个地方(样式)进行此操作,这样便无需在全部代码中进行搜索和替换。

文档级自定义项示例

使用文档级自定义项设置文本格式

  • 下面的示例可用于文档级自定义项。若要使用此代码,请从项目内的 ThisDocument 类中运行此代码。

    Private Sub RangeFormat()
    
        ' Set the Range to the first paragraph.
        Dim rng As Word.Range = Me.Paragraphs(1).Range
    
        ' Change the formatting.
        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. 
        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.
        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. 
        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 文档中插入文本

如何:在文档中搜索文本