以程式設計方式將文件屬性填入 Word 表格

下列範例會在文件的頂端建立 Microsoft Office Word 表格,並用主文件的屬性填入這個表格。

適用對象:本主題資訊適用於文件層級的專案和 Word 的 VSTO 增益集專案。 如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

在文件層級自訂中填入表格

建立表格並且用文件的屬性填入這個表格

  1. 將範圍設為文件頂端。

    object start = 0, end = 0; 
    Word.Range rng = this.Range(ref start, ref end);
    
  2. 插入表格標題並且包含段落標記。

    rng.InsertBefore("Document Statistics"); 
    rng.Font.Name = "Verdana"; 
    rng.Font.Size = 16; 
    rng.InsertParagraphAfter(); 
    rng.InsertParagraphAfter(); 
    rng.SetRange(rng.End, rng.End);
    
  3. 在文件的該範圍內加入表格。

    rng.Tables.Add(this.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. 格式化表格並套用樣式。

    Word.Table tbl = this.Tables[1];
    tbl.Range.Font.Size = 12; 
    tbl.Columns.DistributeWidth(); 
    
    object styleName = "Table Professional";
    tbl.set_Style(ref styleName);
    
  5. 將文件屬性插入儲存格。

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

    下列範例示範完整的程序。 若要使用此程式碼,請從專案的 ThisDocument 類別中執行它。

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

在 VSTO 增益集中填入表格

建立表格並且用文件的屬性填入這個表格

  1. 將範圍設為文件頂端。

    object start = 0, end = 0;
    Word.Document document = this.Application.ActiveDocument;
    Word.Range rng = document.Range(ref start, ref end);
    
  2. 插入表格標題並且包含段落標記。

    rng.InsertBefore("Document Statistics");
    rng.Font.Name = "Verdana";
    rng.Font.Size = 16;
    rng.InsertParagraphAfter();
    rng.InsertParagraphAfter();
    rng.SetRange(rng.End, rng.End);
    
  3. 在文件的該範圍內加入表格。

    rng.Tables.Add(document.Paragraphs[2].Range, 3, 2, ref missing, ref missing);
    
  4. 格式化表格並套用樣式。

    Word.Table tbl = document.Tables[1];
    tbl.Range.Font.Size = 12;
    tbl.Columns.DistributeWidth();
    
    object styleName = "Table Professional";
    tbl.set_Style(ref styleName);
    
  5. 將文件屬性插入儲存格。

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

    下列範例示範完整的程序。 若要使用此程式碼,請從專案的 ThisAddIn 類別中執行它。

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