Procedura: aggiungere intestazioni e piè di pagina ai documenti

Aggiornamento: novembre 2007

Si applica a

Le informazioni contenute in questo argomento riguardano solo i progetti Visual Studio Tools per Office e le versioni di Microsoft Office specificati.

Tipo di progetto

  • Progetti a livello di documento

  • Progetti a livello di applicazione

Versione Microsoft Office

  • Word 2003

  • Word 2007

Per ulteriori informazioni, vedere la classe Funzionalità disponibili in base ai tipi di progetto e applicazione.

È possibile aggiungere testo a intestazioni e piè di pagina del documento utilizzando le proprietà Headers e Footers della classe Section. Ogni sezione di un documento contiene tre intestazioni e piè di pagina:

Le procedure relative alle personalizzazioni a livello di documento e ai componenti aggiuntivi a livello di applicazione sono differenti.

Personalizzazioni a livello di documento

Per aggiungere testo ai piè di pagina nel documento

  1. Impostare il tipo di carattere del testo da inserire nel piè di pagina principale di ogni sezione del documento.

    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. Inserire il testo nel piè di pagina.

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

Per aggiungere testo alle intestazioni nel documento

  1. Aggiungere una voce AutoText per visualizzare il testo Page X of Y in ogni intestazione del documento.

    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. Impostare l'allineamento dei paragrafi in modo che il testo venga allineato a destra dell'intestazione.

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

Compilazione del codice

Per utilizzare questi esempi di codice, eseguirli dall'interno della classe ThisDocument nel progetto.

Componenti aggiuntivi a livello di applicazione

Per aggiungere testo ai piè di pagina di un documento

  1. Impostare il tipo di carattere del testo da inserire nel piè di pagina principale di ogni sezione del documento. In questo esempio di codice viene utilizzato il documento attivo.

    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. Inserire il testo nel piè di pagina.

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

Per aggiungere testo alle intestazioni nel documento

  1. Aggiungere una voce AutoText per visualizzare il testo Page X of Y in ogni intestazione del documento. In questo esempio di codice viene utilizzato il documento attivo.

    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. Impostare l'allineamento dei paragrafi in modo che il testo venga allineato a destra dell'intestazione.

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

Compilazione del codice

Per utilizzare questi esempi di codice, eseguirli dall'interno della classe ThisAddIn nel progetto.

Vedere anche

Attività

Procedura: creare nuovi documenti

Procedura: estendere gli intervalli nei documenti

Procedura: scorrere in ciclo gli elementi trovati nei documenti