Remarque
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
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
Version de Microsoft Office
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
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 = 20foreach (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;Insérez le texte dans le pied de page.
section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _ .Range.Text = "Confidential" NextwordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary] .Range.Text = "Confidential"; }
Pour ajouter du texte aux en-têtes dans le document
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);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 Nextsection.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
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 = 20Word.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;Insérez le texte dans le pied de page.
section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _ .Range.Text = "Confidential" NextwordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary] .Range.Text = "Confidential"; }
Pour ajouter du texte aux en-têtes dans le document
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);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 Nextsection.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