Programmgesteuertes Auffüllen von Word-Tabellen mit Dokumenteigenschaften

Im folgenden Beispiel wird eine Microsoft Office Word-Tabelle am Anfang des Dokuments erstellt und mit den Eigenschaften des Hostdokuments aufgefüllt.

Gilt für: Die Informationen in diesem Thema gelten für Projekte auf Dokumentebene und VSTO-Add-In-Projekte für Word. Weitere Informationen finden Sie unter features available by Office-App lication and project type.

Auffüllen von Tabellen in einer Anpassung auf Dokumentebene

So erstellen Sie eine Tabelle und füllen sie mit Dokumenteigenschaften auf

  1. Legen Sie den Bereich auf den Anfang des Dokuments fest.

    object start = 0, end = 0; 
    Word.Range rng = this.Range(ref start, ref end);
    
  2. Fügen Sie einen Titel für die Tabelle ein, und schließen Sie Absatzmarken ein.

    rng.InsertBefore("Document Statistics"); 
    rng.Font.Name = "Verdana"; 
    rng.Font.Size = 16; 
    rng.InsertParagraphAfter(); 
    rng.InsertParagraphAfter(); 
    rng.SetRange(rng.End, rng.End);
    
  3. Fügen Sie die Tabelle dem Bereich im Dokument hinzu.

    rng.Tables.Add(this.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. Formatieren Sie die Tabelle, und wenden Sie ein Format an.

    Word.Table tbl = this.Tables[1];
    tbl.Range.Font.Size = 12; 
    tbl.Columns.DistributeWidth(); 
    
    object styleName = "Table Professional";
    tbl.set_Style(ref styleName);
    
  5. Fügen Sie die Dokumenteigenschaften in Zellen ein.

    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();
    

    Im folgenden Beispiel wird das vollständige Verfahren veranschaulicht. Wenn Sie diesen Code verwenden möchten, führen Sie ihn von der ThisDocument -Klasse im Projekt aus.

    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();
    }
    

Auffüllen von Tabellen in einem VSTO-Add-In

So erstellen Sie eine Tabelle und füllen sie mit Dokumenteigenschaften auf

  1. Legen Sie den Bereich auf den Anfang des Dokuments fest.

    object start = 0, end = 0;
    Word.Document document = this.Application.ActiveDocument;
    Word.Range rng = document.Range(ref start, ref end);
    
  2. Fügen Sie einen Titel für die Tabelle ein, und schließen Sie Absatzmarken ein.

    rng.InsertBefore("Document Statistics");
    rng.Font.Name = "Verdana";
    rng.Font.Size = 16;
    rng.InsertParagraphAfter();
    rng.InsertParagraphAfter();
    rng.SetRange(rng.End, rng.End);
    
  3. Fügen Sie die Tabelle dem Bereich im Dokument hinzu.

    rng.Tables.Add(document.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. Formatieren Sie die Tabelle, und wenden Sie ein Format an.

    Word.Table tbl = document.Tables[1];
    tbl.Range.Font.Size = 12;
    tbl.Columns.DistributeWidth();
    
    object styleName = "Table Professional";
    tbl.set_Style(ref styleName);
    
  5. Fügen Sie die Dokumenteigenschaften in Zellen ein.

    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();
    

    Im folgenden Beispiel wird das vollständige Verfahren veranschaulicht. Wenn Sie diesen Code verwenden möchten, führen Sie ihn von der ThisAddIn -Klasse im Projekt aus.

    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();
    }