HOW TO:將文件屬性填入 Word 表格
更新:2007 年 11 月
適用於 |
---|
本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。 專案類型
Microsoft Office 版本
如需詳細資訊,請參閱依應用程式和專案類型提供的功能。 |
下列範例在文件的頂端建立 Microsoft Office Word 表格,並用主文件的屬性填入 (Populate) 這個表格。
在文件層級自訂中填入表格
若要建立表格並且用文件的屬性填入這個表格
將範圍設為文件頂端。
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);
插入表格標題並且包含段落標記。
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);
在文件的該範圍內加入表格。
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);
格式化表格並套用樣式。
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);
將文件屬性插入儲存格。
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();
下列範例顯示完整的程序。若要使用這個程式碼,請從專案中的 ThisDocument 類別中執行。
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();
}
在應用程式層級增益集中填入表格
若要建立表格並且用文件的屬性填入這個表格
將範圍設為文件頂端。
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);
插入表格標題並且包含段落標記。
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);
在文件的該範圍內加入表格。
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);
格式化表格並套用樣式。
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);
將文件屬性插入儲存格。
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();
下列範例顯示完整的程序。若要使用這個程式碼,請從專案中的 ThisAddIn 類別中執行。
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();
}