Remplir par programmation des tables Word avec des propriétés de document

L'exemple suivant crée un tableau Microsoft Office Word en haut du document et le remplit avec les propriétés du document hôte.

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.

Remplir des tables dans une personnalisation au niveau du document

Pour créer un tableau et le remplir avec des propriétés de document

  1. Définissez la plage en haut du document.

    object start = 0, end = 0; 
    Word.Range rng = this.Range(ref start, ref end);
    
  2. Insérez un titre pour le tableau et insérez des marques de paragraphe.

    rng.InsertBefore("Document Statistics"); 
    rng.Font.Name = "Verdana"; 
    rng.Font.Size = 16; 
    rng.InsertParagraphAfter(); 
    rng.InsertParagraphAfter(); 
    rng.SetRange(rng.End, rng.End);
    
  3. Ajoutez le tableau au document au niveau de la plage.

    rng.Tables.Add(this.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. Mettez en forme le tableau et appliquez un style.

    Word.Table tbl = this.Tables[1];
    tbl.Range.Font.Size = 12; 
    tbl.Columns.DistributeWidth(); 
    
    object styleName = "Table Professional";
    tbl.set_Style(ref styleName);
    
  5. Insérez les propriétés de document dans les cellules.

    tbl.Cell(1, 1).Range.Text = "Document Property";
    tbl.Cell(1, 2).Range.Text = "Value";
    
    tbl.Cell(2, 1).Range.Text = "Subject";
    tbl.Cell(2, 2).Range.Text = ((Office.DocumentProperties)(this.BuiltInDocumentProperties))
        [Word.WdBuiltInProperty.wdPropertySubject].Value.ToString();
    
    tbl.Cell(3, 1).Range.Text = "Author";
    tbl.Cell(3, 2).Range.Text = ((Office.DocumentProperties)(this.BuiltInDocumentProperties))
        [Word.WdBuiltInProperty.wdPropertyAuthor].Value.ToString();
    

    L'exemple suivant montre la procédure complète. Pour utiliser ce code, exécutez-le à partir de la classe ThisDocument de votre projet.

    private void CreateDocumentPropertyTable() 
    { 
        object start = 0, end = 0; 
        Word.Range rng = this.Range(ref start, ref end); 
    
        // Insert a title for the table and paragraph marks. 
        rng.InsertBefore("Document Statistics"); 
        rng.Font.Name = "Verdana"; 
        rng.Font.Size = 16; 
        rng.InsertParagraphAfter(); 
        rng.InsertParagraphAfter(); 
        rng.SetRange(rng.End, rng.End); 
    
        // Add the table.
        rng.Tables.Add(this.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
        // Format the table and apply a style. 
        Word.Table tbl = this.Tables[1];
        tbl.Range.Font.Size = 12; 
        tbl.Columns.DistributeWidth(); 
    
        object styleName = "Table Professional";
        tbl.set_Style(ref styleName); 
    
        // Insert document properties into cells. 
        tbl.Cell(1, 1).Range.Text = "Document Property";
        tbl.Cell(1, 2).Range.Text = "Value";
    
        tbl.Cell(2, 1).Range.Text = "Subject";
        tbl.Cell(2, 2).Range.Text = ((Office.DocumentProperties)(this.BuiltInDocumentProperties))
            [Word.WdBuiltInProperty.wdPropertySubject].Value.ToString();
    
        tbl.Cell(3, 1).Range.Text = "Author";
        tbl.Cell(3, 2).Range.Text = ((Office.DocumentProperties)(this.BuiltInDocumentProperties))
            [Word.WdBuiltInProperty.wdPropertyAuthor].Value.ToString();
    }
    

Remplir des tables dans un complément VSTO

Pour créer un tableau et le remplir avec des propriétés de document

  1. Définissez la plage en haut du document.

    object start = 0, end = 0;
    Word.Document document = this.Application.ActiveDocument;
    Word.Range rng = document.Range(ref start, ref end);
    
  2. Insérez un titre pour le tableau et insérez des marques de paragraphe.

    rng.InsertBefore("Document Statistics");
    rng.Font.Name = "Verdana";
    rng.Font.Size = 16;
    rng.InsertParagraphAfter();
    rng.InsertParagraphAfter();
    rng.SetRange(rng.End, rng.End);
    
  3. Ajoutez le tableau au document au niveau de la plage.

    rng.Tables.Add(document.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. Mettez en forme le tableau et appliquez un style.

    Word.Table tbl = document.Tables[1];
    tbl.Range.Font.Size = 12;
    tbl.Columns.DistributeWidth();
    
    object styleName = "Table Professional";
    tbl.set_Style(ref styleName);
    
  5. Insérez les propriétés de document dans les cellules.

    tbl.Cell(1, 1).Range.Text = "Document Property";
    tbl.Cell(1, 2).Range.Text = "Value";
    
    tbl.Cell(2, 1).Range.Text = "Subject";
    tbl.Cell(2, 2).Range.Text = ((Office.DocumentProperties)(document.BuiltInDocumentProperties))
        [Word.WdBuiltInProperty.wdPropertySubject].Value.ToString();
    
    tbl.Cell(3, 1).Range.Text = "Author";
    tbl.Cell(3, 2).Range.Text = ((Office.DocumentProperties)(document.BuiltInDocumentProperties))
        [Word.WdBuiltInProperty.wdPropertyAuthor].Value.ToString();
    

    L'exemple suivant montre la procédure complète. Pour utiliser ce code, exécutez-le à partir de la classe ThisAddIn de votre projet.

    private void CreateDocumentPropertyTable()
    {
        object start = 0, end = 0;
        Word.Document document = this.Application.ActiveDocument;
        Word.Range rng = document.Range(ref start, ref end);
    
        // Insert a title for the table and paragraph marks. 
        rng.InsertBefore("Document Statistics");
        rng.Font.Name = "Verdana";
        rng.Font.Size = 16;
        rng.InsertParagraphAfter();
        rng.InsertParagraphAfter();
        rng.SetRange(rng.End, rng.End);
    
        // Add the table.
        rng.Tables.Add(document.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
        // Format the table and apply a style. 
        Word.Table tbl = document.Tables[1];
        tbl.Range.Font.Size = 12;
        tbl.Columns.DistributeWidth();
    
        object styleName = "Table Professional";
        tbl.set_Style(ref styleName);
    
        // Insert document properties into cells. 
        tbl.Cell(1, 1).Range.Text = "Document Property";
        tbl.Cell(1, 2).Range.Text = "Value";
    
        tbl.Cell(2, 1).Range.Text = "Subject";
        tbl.Cell(2, 2).Range.Text = ((Office.DocumentProperties)(document.BuiltInDocumentProperties))
            [Word.WdBuiltInProperty.wdPropertySubject].Value.ToString();
    
        tbl.Cell(3, 1).Range.Text = "Author";
        tbl.Cell(3, 2).Range.Text = ((Office.DocumentProperties)(document.BuiltInDocumentProperties))
            [Word.WdBuiltInProperty.wdPropertyAuthor].Value.ToString();
    }