Comment : remplir par programmation des tableaux Word avec des propriétés de documentHow to: Programmatically populate Word tables with document properties

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.The following example creates a Microsoft Office Word table at the top of the document and populates it with the properties of the host document.

S’applique à : les informations contenues dans cette rubrique s’applique au document-projets et ajouter de VSTO de niveau-dans les projets pour Word.Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Word. Pour plus d’informations, consultez fonctionnalités disponibles par type d’application et de projet Office.For more information, see Features available by Office application and project type.

Remplir les tables dans une personnalisation au niveau du documentPopulate tables in a document-level customization

Pour créer un tableau et le remplir avec des propriétés de documentTo create a table and populate it with document properties

  1. Définissez la plage en haut du document.Set the range to the top of the document.

    Dim rng As Word.Range = Me.Range(Start:=0, End:=0)
    
    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.Insert a title for the table and include paragraph marks.

    With rng
        .InsertBefore("Document Statistics")
        .Font.Name = "Verdana"
        .Font.Size = 16
        .InsertParagraphAfter()
        .InsertParagraphAfter()
        .SetRange(rng.End, rng.End)
    End With
    
    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.Add the table to the document at the range.

    rng.Tables.Add(Range:=Me.Paragraphs.Item(2).Range, NumRows:=3, NumColumns:=2)
    
    rng.Tables.Add(this.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. Mettez en forme le tableau et appliquez un style.Format the table and apply a style.

    With Me.Tables.Item(1)
        .Range.Font.Size = 12
        .Columns.DistributeWidth()
        .Style = "Table Professional"
    End With
    
    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.Insert the document properties into cells.

    With Me.Tables.Item(1)
        .Cell(1, 1).Range.Text = "Document Property"
        .Cell(1, 2).Range.Text = "Value"
    
        .Cell(2, 1).Range.Text = "Subject"
        .Cell(2, 2).Range.Text = CType(Me.BuiltInDocumentProperties, Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertySubject).Value.ToString()
    
        .Cell(3, 1).Range.Text = "Author"
        .Cell(3, 2).Range.Text = CType(Me.BuiltInDocumentProperties, Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertyAuthor).Value.ToString()
    End With
    
    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.The following example shows the complete procedure. Pour utiliser ce code, exécutez-le à partir de la classe ThisDocument de votre projet.To use this code, run it from the ThisDocument class in your project.

    Private Sub CreateDocumentPropertyTable()
        Dim rng As Word.Range = Me.Range(Start:=0, End:=0)
    
        ' Insert a title for the table and paragraph marks.
        With rng
            .InsertBefore("Document Statistics")
            .Font.Name = "Verdana"
            .Font.Size = 16
            .InsertParagraphAfter()
            .InsertParagraphAfter()
            .SetRange(rng.End, rng.End)
        End With
    
        ' Add the table.
        rng.Tables.Add(Range:=Me.Paragraphs.Item(2).Range, NumRows:=3, NumColumns:=2)
    
        ' Format the table and apply a style.
        With Me.Tables.Item(1)
            .Range.Font.Size = 12
            .Columns.DistributeWidth()
            .Style = "Table Professional"
        End With
    
        ' Insert document properties into cells.
        With Me.Tables.Item(1)
            .Cell(1, 1).Range.Text = "Document Property"
            .Cell(1, 2).Range.Text = "Value"
    
            .Cell(2, 1).Range.Text = "Subject"
            .Cell(2, 2).Range.Text = CType(Me.BuiltInDocumentProperties, Office.DocumentProperties) _
                (Word.WdBuiltInProperty.wdPropertySubject).Value.ToString()
    
            .Cell(3, 1).Range.Text = "Author"
            .Cell(3, 2).Range.Text = CType(Me.BuiltInDocumentProperties, Office.DocumentProperties) _
                (Word.WdBuiltInProperty.wdPropertyAuthor).Value.ToString()
        End With
    End Sub
    
    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 les tables dans un composant logiciel complément VSTOPopulate tables in a VSTO Add-in

Pour créer un tableau et le remplir avec des propriétés de documentTo create a table and populate it with document properties

  1. Définissez la plage en haut du document.Set the range to the top of the document.

    Dim rng As Word.Range = Me.Application.ActiveDocument.Range( _
        Start:=0, End:=0)
    
    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.Insert a title for the table and include paragraph marks.

    With rng
        .InsertBefore("Document Statistics")
        .Font.Name = "Verdana"
        .Font.Size = 16
        .InsertParagraphAfter()
        .InsertParagraphAfter()
        .SetRange(rng.End, rng.End)
    End With
    
    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.Add the table to the document at the range.

    rng.Tables.Add(Range:=Me.Application.ActiveDocument.Paragraphs.Item(2).Range, _
        NumRows:=3, NumColumns:=2)
    
    rng.Tables.Add(document.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. Mettez en forme le tableau et appliquez un style.Format the table and apply a style.

    With Me.Application.ActiveDocument.Tables.Item(1)
        .Range.Font.Size = 12
        .Columns.DistributeWidth()
        .Style = "Table Professional"
    End With
    
    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.Insert the document properties into cells.

    With Me.Application.ActiveDocument.Tables.Item(1)
        .Cell(1, 1).Range.Text = "Document Property"
        .Cell(1, 2).Range.Text = "Value"
    
        .Cell(2, 1).Range.Text = "Subject"
        .Cell(2, 2).Range.Text = CType( _
            Me.Application.ActiveDocument.BuiltInDocumentProperties,  _
            Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertySubject).Value.ToString()
    
        .Cell(3, 1).Range.Text = "Author"
        .Cell(3, 2).Range.Text = CType( _
            Me.Application.ActiveDocument.BuiltInDocumentProperties,  _
            Office.DocumentProperties) _
            (Word.WdBuiltInProperty.wdPropertyAuthor).Value.ToString()
    End With
    
    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.The following example shows the complete procedure. Pour utiliser ce code, exécutez-le à partir de la classe ThisAddIn de votre projet.To use this code, run it from the ThisAddIn class in your project.

    Private Sub CreateDocumentPropertyTable()
        Dim rng As Word.Range = Me.Application.ActiveDocument.Range( _
            Start:=0, End:=0)
    
        ' Insert a title for the table and paragraph marks.
        With rng
            .InsertBefore("Document Statistics")
            .Font.Name = "Verdana"
            .Font.Size = 16
            .InsertParagraphAfter()
            .InsertParagraphAfter()
            .SetRange(rng.End, rng.End)
        End With
    
        ' Add the table.
        rng.Tables.Add(Range:=Me.Application.ActiveDocument.Paragraphs.Item(2).Range, _
            NumRows:=3, NumColumns:=2)
    
        ' Format the table and apply a style.
        With Me.Application.ActiveDocument.Tables.Item(1)
            .Range.Font.Size = 12
            .Columns.DistributeWidth()
            .Style = "Table Professional"
        End With
    
        ' Insert document properties into cells.
        With Me.Application.ActiveDocument.Tables.Item(1)
            .Cell(1, 1).Range.Text = "Document Property"
            .Cell(1, 2).Range.Text = "Value"
    
            .Cell(2, 1).Range.Text = "Subject"
            .Cell(2, 2).Range.Text = CType( _
                Me.Application.ActiveDocument.BuiltInDocumentProperties,  _
                Office.DocumentProperties) _
                (Word.WdBuiltInProperty.wdPropertySubject).Value.ToString()
    
            .Cell(3, 1).Range.Text = "Author"
            .Cell(3, 2).Range.Text = CType( _
                Me.Application.ActiveDocument.BuiltInDocumentProperties,  _
                Office.DocumentProperties) _
                (Word.WdBuiltInProperty.wdPropertyAuthor).Value.ToString()
        End With
    End Sub
    
    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();
    }
    

Voir aussiSee also

Comment : créer des tableaux Word par programmation How to: Programmatically create Word tables
Comment : ajouter texte et mise en forme aux cellules des tableaux Word par programmation How to: Programmatically add text and formatting to cells in Word tables
Comment : ajouter par programmation des lignes et colonnes à des tableaux Word How to: Programmatically add rows and columns to Word tables
Paramètres optionnels dans les solutions OfficeOptional parameters in Office solutions