Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Word. For more information, see Features available by Office application and project type.
Populate tables in a document-level customization
To create a table and populate it with document properties
Set the range to the top of the document.
Insert a title for the table and include paragraph marks.
Add the table to the document at the range.
Format the table and apply a style.
Insert the 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();
The following example shows the complete procedure. To use this code, run it from the
ThisDocument
class in your project.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(); }
Populate tables in a VSTO Add-in
To create a table and populate it with document properties
Set the range to the top of the document.
Insert a title for the table and include paragraph marks.
Add the table to the document at the range.
Format the table and apply a style.
Insert the 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();
The following example shows the complete procedure. To use this code, run it from the
ThisAddIn
class in your project.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(); }