Mettre en forme par programmation du texte dans des documents

Vous pouvez utiliser l'objet Range pour mettre en forme le texte dans un document Microsoft Office Word.

S’applique à : les informations contenues dans cette rubrique s’appliquent aux projets au niveau du document et aux projets de complément VSTO pour Word. Pour plus d’informations, consultez Fonctionnalités disponibles par application Office lication et le type de projet.

L'exemple suivant sélectionne le premier paragraphe du document et modifie la taille de la police, le nom de la police et l'alignement. Ensuite, il sélectionne la plage et affiche un message d'interruption avant l'exécution de la section de code suivante. La section suivante appelle la méthode Undo de l’élément Document hôte (pour une personnalisation au niveau du document) ou la Document classe (pour un complément VSTO) trois fois. Il applique le style Retrait normal et affiche un message permettant de suspendre le code. Puis, le code appelle la méthode Undo une seule fois et affiche un message.

Exemple de personnalisation au niveau du document

Pour mettre en forme un texte à l'aide d'une personnalisation au niveau du document

  1. L'exemple suivant peut être utilisé dans une personnalisation au niveau du document. Pour utiliser ce code, exécutez-le à partir de la classe ThisDocument de votre projet.

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

Exemple de complément VSTO

Pour mettre en forme du texte avec un complément VSTO

  1. L’exemple suivant peut être utilisé dans un complément VSTO. Cet exemple utilise le document actif. Pour utiliser ce code, exécutez-le à partir de la classe ThisAddIn de votre projet.

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