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

Mise à jour : novembre 2007

S'applique à

Les informations de cette rubrique s'appliquent uniquement aux projets Visual Studio Tools pour Office et versions de Microsoft Office spécifiés.

Type de projet

  • Projets au niveau du document

  • Projets au niveau de l'application

Version de Microsoft Office

  • Word 2003

  • Word 2007

Pour plus d'informations, consultez Fonctionnalités disponibles par type d'application et de projet.

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.

Personnalisations au niveau du document

Pour ajouter du texte aux pieds de page dans le document

  1. Définissez la police du texte à insérer dans le pied de page primaire de chaque section du document.

    Dim section As Word.Section
    
    For Each section In Me.Sections
        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
    
        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Font.Size = 20
    
    foreach (Word.Section wordSection in this.Sections)
    {
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
    
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Font.Size = 20;
    
  2. Insérez le texte dans le pied de page.

        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Text = "Confidential"
    Next
    
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Text = "Confidential";
    }
    

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

  1. Ajoutez une insertion automatique pour afficher Page X sur Y dans chaque en-tête dans le document.

    Dim section As Word.Section
    For Each section In Me.Sections
    
        section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Add( _
            section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, _
            Word.WdFieldType.wdFieldEmpty, "AUTOTEXT  ""Page X of Y"" ", True)
    
    foreach (Word.Section section in this.Sections)
    {
        object fieldEmpty = Word.WdFieldType.wdFieldEmpty;
        object autoText = "AUTOTEXT  \"Page X of Y\" ";
        object preserveFormatting = true;
    
        section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(
            section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,
            ref fieldEmpty, ref autoText, ref preserveFormatting);
    
  2. Définissez l'alignement du paragraphe afin que le texte s'aligne à droite de l'en-tête.

        section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    Next
    
        section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    }
    

Compilation du code

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

Compléments au niveau de l'application

Pour ajouter du texte aux pieds de page dans un document

  1. Définissez la police du texte à insérer dans le pied de page primaire de chaque section du document. Cet exemple de code utilise le document actif.

    Dim section As Word.Section
    Dim document As Word.Document = Me.Application.ActiveDocument
    
    For Each section In document.Sections
        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
    
        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Font.Size = 20
    
    Word.Document document = this.Application.ActiveDocument;
    foreach (Word.Section wordSection in document.Sections)
    {
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
    
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Font.Size = 20;
    
  2. Insérez le texte dans le pied de page.

        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Text = "Confidential"
    Next
    
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Text = "Confidential";
    }
    

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

  1. Ajoutez une insertion automatique pour afficher Page X sur Y dans chaque en-tête dans le document. Cet exemple de code utilise le document actif.

    Dim section As Word.Section
    For Each section In Me.Application.ActiveDocument.Sections
    
        section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Add( _
            section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, _
            Word.WdFieldType.wdFieldEmpty, "AUTOTEXT  ""Page X of Y"" ", True)
    
    foreach (Word.Section section in this.Application.ActiveDocument.Sections)
    {
        object fieldEmpty = Word.WdFieldType.wdFieldEmpty;
        object autoText = "AUTOTEXT  \"Page X of Y\" ";
        object preserveFormatting = true;
    
        section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(
            section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,
            ref fieldEmpty, ref autoText, ref preserveFormatting);
    
  2. Définissez l'alignement du paragraphe afin que le texte s'aligne à droite de l'en-tête.

        section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    Next
    
        section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    }
    

Compilation du code

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

Voir aussi

Tâches

Comment : créer de nouveaux documents

Comment : étendre des plages dans des documents

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