本文將逐步告訴您,如何呼叫 Office 巨集從 Visual Basic.NET 自動化用戶端。
您可以使用 Microsoft Office 自動化在開啟文件或建立新的文件包含的應用程式 VBA 巨集的 Visual Basic 和執行巨集在執行階段。
下列範例自動化用戶端管理 Office 自動化伺服器 (Access、 Excel、 PowerPoint 或 Word) 根據您在表單上的選取。用戶端會啟動 「 自動化 」 伺服器之後它會開啟文件,並接著會呼叫兩個巨集。第一個巨集 DoKbTest,沒有參數,而第二個巨集 DoKbTestWithParameter,採用單一參數的型別
字串。
建立包含巨集的 Office 文件
- 建立名為 C:\Doc1.doc 的 Word 文件。要這麼做,請您執行下列步驟:
- 在 Word 中, 建立新文件。
- 按下 ALT + F11 開啟 Visual Basic 編輯器。
- 在 插入] 功能表上按一下 [模組]。
- 下列的巨集程式碼貼入新模組:
'Display a message box that displays the application name.
Public Sub DoKbTest()
MsgBox "Hello from " & Application.Name
End Sub
'Display a message box with the string passed from the
'Automation client.
Public Sub DoKbTestWithParameter( sMsg As String )
MsgBox sMsg
End Sub
- 關閉 [Visual Basic 編輯程式、 儲存在 Word 文件,並結束 Word。
- 建立 Excel 活頁簿名稱 C:\Book1.xls 為使用步驟類似於那些您用來建立 Word 文件。
- 建立 PowerPoint 簡報名 C:\Pres1.ppt 為使用步驟類似於那些您用來建立 [Word 文件。
- 建立新的 Access 資料庫,名為 C:\Db1.mdb。要這麼做,請您執行下列步驟:
- 在 插入] 功能表上按一下 [模組]。
- 巨集程式碼貼入新的模組中。
- 儲存模組,並結束 Access。
建立 Visual Basic.NET 自動化用戶端
- 啟動 Microsoft Visual Studio.NET。在 [檔案] 功能表上按一下 [新增],然後按一下 [專案]。選取 [從 Visual Basic 專案類型的 [Windows 應用程式]。預設會建立 Form1。
- 加入存取、 Excel、 PowerPoint 及 Word 物件程式庫的參考。要這麼做,請您執行下列步驟:
- 在 [專案] 功能表上按一下 [加入參考]。
- 在 [COM] 索引標籤上找到 Microsoft Word 10.0 物件程式庫或 Microsoft Word 11.0 物件程式庫,然後按一下 [選取]。
附註如果您使用 Microsoft Office XP,而且您已經不這樣做,Microsoft 建議您下載並安裝組 [Microsoft Office XP 主要 Interop 件 (PIA)。 如需有關 Office XP PIA,按一下 [下面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項]:328912?
(http://support.microsoft.com/kb/328912/
)
Microsoft Office XP 主要 Interop 組件 (PIA) 是可供下載
- 重複先前步驟中,存取、 Excel 及 PowerPoint 物件程式庫的。
- 按一下 [[確定] 在 [加入參考] 對話方塊以接受您的選擇。如果接到提示,以產生您所選取的程式庫的包裝函式時,按一下 [是]。
附註如果參照 [存取] 時收到錯誤訊息 10.0 物件程式庫,請參閱 < 疑難排解 > 一節。
- 按一下 [檢視] 功能表 工具箱。將下拉式方塊和按鈕加入至 Form1。
- 連按兩下 [Button1] 來產生一個定義為按鈕的 Click 事件處理常式。
- 下列程式碼貼 Button1_Click 程序中:
Select Case ComboBox1.SelectedItem
Case "Access"
Dim oAccess As Access.ApplicationClass
'Start Access and open the database.
oAccess = CreateObject("Access.Application")
oAccess.Visible = True
oAccess.OpenCurrentDatabase("c:\db1.mdb", False)
'Run the macros.
oAccess.Run ("DoKbTest")
oAccess.Run("DoKbTestWithParameter", "Hello from VB .NET Client")
'Clean-up: Quit Access without saving changes to the database.
oAccess.DoCmd().Quit (Access.AcQuitOption.acQuitSaveNone)
System.Runtime.InteropServices.Marshal.ReleaseComObject (oAccess)
oAccess = Nothing
Case "Excel"
Dim oExcel As Excel.ApplicationClass
Dim oBook As Excel.WorkbookClass
Dim oBooks As Excel.Workbooks
'Start Excel and open the workbook.
oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oBooks = oExcel.Workbooks
oBook = oBooks.Open("c:\book1.xls")
'Run the macros.
oExcel.Run ("DoKbTest")
oExcel.Run("DoKbTestWithParameter", "Hello from VB .NET Client")
'Clean-up: Close the workbook and quit Excel.
oBook.Close (False)
System.Runtime.InteropServices.Marshal.ReleaseComObject (oBook)
oBook = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject (oBooks)
oBooks = Nothing
oExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject (oExcel)
oExcel = Nothing
Case "PowerPoint"
Dim oPP As PowerPoint.ApplicationClass
Dim oPresSet As PowerPoint.Presentations
Dim oPres As PowerPoint.PresentationClass
'Start PowerPoint and open the presentation.
oPP = CreateObject("PowerPoint.Application")
oPP.Visible = True
oPresSet = oPP.Presentations
oPres = oPresSet.Open("c:\pres1.ppt", , , True)
'Run the macros.
oPP.Run ("'pres1.ppt'!DoKbTest")
oPP.Run("'pres1.ppt'!DoKbTestWithParameter", "Hello from VB .NET Client")
'Clean-up: Close the presentation and quit PowerPoint.
oPres.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject (oPres)
oPres = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject (oPresSet)
oPresSet = Nothing
oPP.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject (oPP)
oPP = Nothing
Case "Word"
Dim oWord As Word.ApplicationClass
'Start Word and open the document.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Open ("C:\Doc1.doc")
'Run the macros.
oWord.Run ("DoKbTest")
oWord.Run("DoKbTestWithParameter", "Hello from VB .NET Client")
'Quit Word.
oWord.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWord)
oWord = Nothing
End Select
GC.Collect() - 在 [檢視] 功能表上按一下 [設計工具],然後連按兩下 Form1 來產生表單的 Load 事件的定義。
- 下列程式碼貼 Form1_Load 程序中:
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
Dim a As String() = {"Access", "Excel", "PowerPoint", "Word"}
ComboBox1.Items.AddRange(a)
ComboBox1.SelectedIndex = 0
- 將下列程式碼加入至 Form1.vb 的頂端:
Imports Access = Microsoft.Office.Interop.Access
Imports Excel = Microsoft.Office.Interop.Excel
Imports Word = Microsoft.Office.Interop.Word
Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
執行並測試自動化用戶端
- 按下 F5 以執行應用程式。
- 從 ComboBox1,選取 Office 應用程式,然後再按一下 [Button1]。您所選取之 Office 應用程式啟動,而且 DoKBTest 及 DoKBTestWithParameter 的巨集能夠被執行。
疑難排解
當您參考 Access 10.0 物件程式庫,Visual Basic.NET 專案中的時,您可能會收到錯誤訊息指出媒體櫃轉換至.NET 組件失敗。 如需有關如何解決這個問題,這樣您就可以成功地參考 Access 10.0 物件程式庫的詳細資訊,按一下 [下面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項]:
317157?
(http://support.microsoft.com/kb/317157/
)
當您參考 Access 10.0 型別程式庫與 Visual Studio.NET PRB: 錯誤
如需詳細資訊,請按一下下列的文件編號,檢視 「 Microsoft 知識庫 」 中的文件:
303871?
(http://support.microsoft.com/kb/303871/
)
如何藉由使用 「 Visual Basic.NET 的自動化建立 Excel 巨集
177760?
(http://support.microsoft.com/kb/177760/
)
ACC97: 如何在其他 Office 程式中執行巨集
如需詳細資訊和資源關於 Office 自動化,請造訪下列 Microsoft 網站: