下面的示例在文档的顶部创建 Microsoft Office Word 表格,并使用主机文档的属性填充它。
适用于: 本主题中的信息适用于 Word 的文档级项目和 VSTO 外接程序项目。 有关详细信息,请参阅办公室应用程序和项目类型提供的功能。
在文档级自定义中填充表
创建表格并使用文档属性填充它
将范围设置为文档的顶部。
插入表格标题,并包括段落标记。
在范围内向文档添加表格。
设置表格格式,并应用样式。
将文档属性插入到单元格。
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 外接程序中填充表
创建表格并使用文档属性填充它
将范围设置为文档的顶部。
插入表格标题,并包括段落标记。
在范围内向文档添加表格。
设置表格格式,并应用样式。
将文档属性插入到单元格。
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(); }