如需本文的 Microsoft Visual C# .NET 版本,請參 閱316384。
如需本文的 Microsoft Visual Basic 6.0 版本,請參 閱313193。
摘要
本逐步說明如何使用 Visual Basic .NET 的 Automation 在 Word 中建立新檔。
範例代碼
本文中的範例程式碼示範如何執行下列動作:
-
插入含有文字和格式設定的段落。
-
流覽及修改檔中的各種範圍。
-
插入表格、設定表格格式,以及在表格中填入資料。
-
新增圖表。
若要使用 Visual Basic .NET 中的 Automation 建立新的 Word 檔,請遵循下列步驟:
-
啟動 Microsoft Visual Studio .NET。 在 [檔案] 功能表中,按一下 [新增],然後按一下 [專案]。 在 [專案類型] 底下,按一下 [Visual Basic Project],然後按一下 [範本] 底下的 [Windows 應用程式]。 表單1 預設為建立。
-
新增 Microsoft Word 物件程式庫的參照。 如果要執行這項操作,請依照下列步驟執行:
-
在 [專案] 功能表上,按一下 [新增參照]。
-
在 [COM] 索引標籤上,找出 Microsoft Word 物件程式庫,然後按一下 [選取]。
請注意,Microsoft Office 2003 及更新版本的 Office 包含主要 Interop 組合 (PIAs) 。 Microsoft Office XP 不包含 PIAs,但可能會下載。 -
按一下 [新增參照] 對話方塊中的 [確定],接受您的選擇。 如果您收到提示,要求您為選取的文件庫產生文繞圖,請按一下 [是]。
-
-
在 [檢視] 功能表上,選取 [工具箱] 以顯示 [工具箱],然後新增按鈕至 [表單1]。
-
按兩下按鈕1。 表單的程式碼視窗隨即出現。
-
在程式碼視窗中,取代下列代碼
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click End Sub
搭配:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim oWord As Word.Application Dim oDoc As Word.Document Dim oTable As Word.Table Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph Dim oRng As Word.Range Dim oShape As Word.InlineShape Dim oChart As Object Dim Pos As Double 'Start Word and open the document template. oWord = CreateObject("Word.Application") oWord.Visible = True oDoc = oWord.Documents.Add 'Insert a paragraph at the beginning of the document. oPara1 = oDoc.Content.Paragraphs.Add oPara1.Range.Text = "Heading 1" oPara1.Range.Font.Bold = True oPara1.Format.SpaceAfter = 24 '24 pt spacing after paragraph. oPara1.Range.InsertParagraphAfter() 'Insert a paragraph at the end of the document. '** \endofdoc is a predefined bookmark. oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range) oPara2.Range.Text = "Heading 2" oPara2.Format.SpaceAfter = 6 oPara2.Range.InsertParagraphAfter() 'Insert another paragraph. oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range) oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:" oPara3.Range.Font.Bold = False oPara3.Format.SpaceAfter = 24 oPara3.Range.InsertParagraphAfter() 'Insert a 3 x 5 table, fill it with data, and make the first row 'bold and italic. Dim r As Integer, c As Integer oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5) oTable.Range.ParagraphFormat.SpaceAfter = 6 For r = 1 To 3 For c = 1 To 5 oTable.Cell(r, c).Range.Text = "r" & r & "c" & c Next Next oTable.Rows.Item(1).Range.Font.Bold = True oTable.Rows.Item(1).Range.Font.Italic = True 'Add some text after the table. 'oTable.Range.InsertParagraphAfter() oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range) oPara4.Range.InsertParagraphBefore() oPara4.Range.Text = "And here's another table:" oPara4.Format.SpaceAfter = 24 oPara4.Range.InsertParagraphAfter() 'Insert a 5 x 2 table, fill it with data, and change the column widths. oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 5, 2) oTable.Range.ParagraphFormat.SpaceAfter = 6 For r = 1 To 5 For c = 1 To 2 oTable.Cell(r, c).Range.Text = "r" & r & "c" & c Next Next oTable.Columns.Item(1).Width = oWord.InchesToPoints(2) 'Change width of columns 1 & 2 oTable.Columns.Item(2).Width = oWord.InchesToPoints(3) 'Keep inserting text. When you get to 7 inches from top of the 'document, insert a hard page break. Pos = oWord.InchesToPoints(7) oDoc.Bookmarks.Item("\endofdoc").Range.InsertParagraphAfter() Do oRng = oDoc.Bookmarks.Item("\endofdoc").Range oRng.ParagraphFormat.SpaceAfter = 6 oRng.InsertAfter("A line of text") oRng.InsertParagraphAfter() Loop While Pos >= oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage) oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd) oRng.InsertBreak(Word.WdBreakType.wdPageBreak) oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd) oRng.InsertAfter("We're now on page 2. Here's my chart:") oRng.InsertParagraphAfter() 'Insert a chart and change the chart. oShape = oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( _ ClassType:="MSGraph.Chart.8", FileName _ :="", LinkToFile:=False, DisplayAsIcon:=False) oChart = oShape.OLEFormat.Object oChart.charttype = 4 'xlLine = 4 oChart.Application.Update() oChart.Application.Quit() 'If desired, you can proceed from here using the Microsoft Graph 'Object model on the oChart object to make additional changes to the 'chart. oShape.Width = oWord.InchesToPoints(6.25) oShape.Height = oWord.InchesToPoints(3.57) 'Add text after the chart. oRng = oDoc.Bookmarks.Item("\endofdoc").Range oRng.InsertParagraphAfter() oRng.InsertAfter("THE END.") 'All done. Close this form. Me.Close() End Sub
-
將下列程式碼新增至 Form1.vb 的頂端:
Imports Word = Microsoft.Office.Interop.Word
-
按 F5 以建立並執行程式。
程式碼完成後,請檢查為您建立的檔。 檔包含兩頁格式化的段落、表格和圖表。
使用範本
如果您使用 Automation 來建立所有採用通用格式的檔,您可以從以預先格式化範本為基礎的新檔開始處理常式獲益。 搭配 Word Automation 用戶端使用範本,比起從無到有建立檔有兩個重要的優點:
-
您可以在檔中進一步控制物件的格式設定和位置。
-
您可以使用較少的程式碼來建立檔。
您可以使用範本微調表格、段落及其他物件在檔中的位置,以及在這些物件上包含格式設定。 使用 Automation,您可以使用下列程式碼,根據範本建立新檔:
oWord.Documents.Add "<Path to your template>\MyTemplate.dot"
在範本中,您可以定義書簽,讓自動化用戶端可以在檔的特定位置填入變數文字,如下所示:
oDoc.Bookmarks.Item("MyBookmark").Range.Text = "Some Text Here"
使用範本的另一個優點是,您可以建立並儲存您想要在執行時間套用的格式設定樣式,如下所示:
oDoc.Bookmarks.Item("MyBookmark").Range.Style = "MyStyle"
- 或 -
oWord.Selection.Style = "MyStyle"
參考
如需使用 Visual Basic .NET 自動化 Microsoft Word 的詳細資訊,請按一下下列文章編號以檢視 Microsoft 知識庫中的文章:
301656 如何將 Word 自動化以從 Visual Basic .NET 執行合併列印