Comment : ajouter des en-têtes et des pieds de page aux documents par programmation

Vous pouvez ajouter du texte aux en-têtes et aux pieds de page dans votre document à l'aide des propriétés Headers et Footers de Section.Chaque section d'un document contient trois en-têtes et pieds de page :

Les procédures pour les personnalisations au niveau du document sont différentes des celles pour les compléments au niveau de l'application.

S'applique à : Les informations contenues dans cette rubrique s'appliquent aux projets de niveau document et de niveau application pour Word 2013 et Word 2010. Pour en savoir plus, consultez Fonctionnalités disponibles par type d'application et de projet Office.

Personnalisations au niveau du document

Pour utiliser les exemples de code suivants, exécutez-les à partir de la classe ThisDocument dans votre projet.

Pour ajouter du texte aux pieds de page dans le document

  • L'exemple de code suivant définit la police du texte à insérer dans le pied de page principal de chaque section du document, puis insère le texte dans le pied de page.

    For Each section As Word.Section In Me.Sections
        Dim footerRange As Word.Range = section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
        footerRange.Font.Size = 20
        footerRange.Text = "Confidential"
    Next
    
    foreach (Word.Section wordSection in this.Sections)
    {
        Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
        footerRange.Font.Size = 20;
        footerRange.Text = "Confidential";
    }
    

Pour ajouter du texte aux en-têtes dans le document

  • L'exemple de code suivant ajoute un champ permettant d'indiquer le numéro de page dans chaque en-tête du document, puis définit l'alignement du paragraphe afin que le texte soit aligné à la droite de l'en-tête.

    For Each section As Word.Section In Me.Sections
        Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage)
        headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    Next
    
    foreach (Word.Section section in this.Sections)
    {
        Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
        headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    }
    

Compléments d'application

Pour utiliser les exemples de code suivants, exécutez-les à partir de la classe ThisAddIn dans votre projet.

Pour ajouter du texte aux pieds de page dans un document

  • L'exemple de code suivant définit la police du texte à insérer dans le pied de page principal de chaque section du document, puis insère le texte dans le pied de page.Cet exemple de code utilise le document actif.

    For Each section As Word.Section In Me.Application.ActiveDocument.Sections
        Dim footerRange As Word.Range = section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
        footerRange.Font.Size = 20
        footerRange.Text = "Confidential"
    Next
    
    foreach (Word.Section wordSection in this.Application.ActiveDocument.Sections)
    {
        Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
        footerRange.Font.Size = 20;
        footerRange.Text = "Confidential";
    }
    

Pour ajouter du texte aux en-têtes dans le document

  • L'exemple de code suivant ajoute un champ permettant d'indiquer le numéro de page dans chaque en-tête du document, puis définit l'alignement du paragraphe afin que le texte soit aligné à la droite de l'en-tête.Cet exemple de code utilise le document actif.

    For Each section As Word.Section In Me.Application.ActiveDocument.Sections
        Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage)
        headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    Next
    
    foreach (Word.Section section in this.Application.ActiveDocument.Sections)
    {
        Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
        headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    }
    

Voir aussi

Tâches

Comment : créer des documents par programmation

Comment : étendre des plages dans des documents par programmation

Comment : parcourir les éléments trouvés dans les documents par programmation