Como: Formatar texto em documentos

Você pode usar o Range o objeto para formatar texto em um documento do Word de Microsoft Office.

Aplicável a: As informações neste tópico se aplicam a projetos de nível de documento e projetos de nível de aplicativo para Word 2007 e Word 2010. Para obter mais informações, consulte Recursos disponíveis pelo aplicativo do Office e o tipo de projeto.

O exemplo a seguir seleciona o primeiro parágrafo do documento e altera o tamanho da fonte, o nome da fonte e o alinhamento. Em seguida, seleciona o intervalo e exibe uma caixa de mensagem para fazer uma pausa antes de executar a próxima seção do código. As chamadas de seção próxima a Undo método o Microsoft.Office.Tools.Word.Document o item de host (para uma personalização em nível de documento) ou o Microsoft.Office.Interop.Word.Document de classe (para um suplemento do nível de aplicativo) três vezes. Ele aplica o estilo Normal recuar e exibe uma caixa de mensagem para pausar o código. Em seguida, o código chama o Undo método uma vez e exibe uma caixa de mensagem.

Exemplo de personalização de nível de documento

Para formatar o texto usando uma personalização em nível de documento

  • O exemplo a seguir pode ser usado em uma personalização em nível de documento. Para usar esse código, executá-lo do ThisDocument classe no projeto.

    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"); 
    }
    

Nível de aplicativo no exemplo de-

Para formatar o texto usando um suplemento em nível de aplicativo

  • O exemplo a seguir pode ser usado em um suplemento em nível de aplicativo. Este exemplo usa o documento ativo. Para usar esse código, executá-lo do ThisAddIn classe no projeto.

    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");
    }
    

Consulte também

Tarefas

Como: Definir e selecionar intervalos em documentos

Como: Inserir texto em documentos do Word

Como: Pesquisa de texto em documentos

Referência

SizeBi()