文章編號: 306682 - 上次校閱: 2007年6月29日 - 版次: 4.6

如何使用 Visual Basic.NET 的自動化執行 Office 的巨集

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
本文章的有 Microsoft Visual C#.NET] 版本請參閱 306683? (http://support.microsoft.com/kb/306683/ )
本文章的有 Microsoft Visual C++] 版本請參閱 306686? (http://support.microsoft.com/kb/306686/ )

在此頁中

全部展開 | 全部摺疊

結論

本文將逐步告訴您,如何呼叫 Office 巨集從 Visual Basic.NET 自動化用戶端。

您可以使用 Microsoft Office 自動化在開啟文件或建立新的文件包含的應用程式 VBA 巨集的 Visual Basic 和執行巨集在執行階段。

其他相關資訊

下列範例自動化用戶端管理 Office 自動化伺服器 (Access、 Excel、 PowerPoint 或 Word) 根據您在表單上的選取。用戶端會啟動 「 自動化 」 伺服器之後它會開啟文件,並接著會呼叫兩個巨集。第一個巨集 DoKbTest,沒有參數,而第二個巨集 DoKbTestWithParameter,採用單一參數的型別 字串

建立包含巨集的 Office 文件

  1. 建立名為 C:\Doc1.doc 的 Word 文件。要這麼做,請您執行下列步驟:
    1. 在 Word 中, 建立新文件。
    2. 按下 ALT + F11 開啟 Visual Basic 編輯器。
    3. 插入] 功能表上按一下 [模組]。
    4. 下列的巨集程式碼貼入新模組:
      '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
    5. 關閉 [Visual Basic 編輯程式、 儲存在 Word 文件,並結束 Word。
  2. 建立 Excel 活頁簿名稱 C:\Book1.xls 為使用步驟類似於那些您用來建立 Word 文件。
  3. 建立 PowerPoint 簡報名 C:\Pres1.ppt 為使用步驟類似於那些您用來建立 [Word 文件。
  4. 建立新的 Access 資料庫,名為 C:\Db1.mdb。要這麼做,請您執行下列步驟:
    1. 插入] 功能表上按一下 [模組]。
    2. 巨集程式碼貼入新的模組中。
    3. 儲存模組,並結束 Access。

建立 Visual Basic.NET 自動化用戶端

  1. 啟動 Microsoft Visual Studio.NET。在 [檔案] 功能表上按一下 [新增],然後按一下 [專案]。選取 [從 Visual Basic 專案類型的 [Windows 應用程式]。預設會建立 Form1。
  2. 加入存取、 Excel、 PowerPoint 及 Word 物件程式庫的參考。要這麼做,請您執行下列步驟:
    1. 在 [專案] 功能表上按一下 [加入參考]。
    2. 在 [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) 是可供下載
    3. 重複先前步驟中,存取、 Excel 及 PowerPoint 物件程式庫的。
    4. 按一下 [[確定] 在 [加入參考] 對話方塊以接受您的選擇。如果接到提示,以產生您所選取的程式庫的包裝函式時,按一下 [是]

      附註如果參照 [存取] 時收到錯誤訊息 10.0 物件程式庫,請參閱 < 疑難排解 > 一節。
  3. 按一下 [檢視] 功能表 工具箱。將下拉式方塊和按鈕加入至 Form1。
  4. 連按兩下 [Button1] 來產生一個定義為按鈕的 Click 事件處理常式。
  5. 下列程式碼貼 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() 
  6. 在 [檢視] 功能表上按一下 [設計工具],然後連按兩下 Form1 來產生表單的 Load 事件的定義。
  7. 下列程式碼貼 Form1_Load 程序中:
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
            Dim a As String() = {"Access", "Excel", "PowerPoint", "Word"}
            ComboBox1.Items.AddRange(a)
            ComboBox1.SelectedIndex = 0
    					
  8. 將下列程式碼加入至 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
    					

執行並測試自動化用戶端

  1. 按下 F5 以執行應用程式。
  2. 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 網站:
與 Visual Studio 的 Microsoft Office 程式開發
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)

常見問題集與 Office 程式開發的反白提示
http://msdn2.microsoft.com/en-us/office/default.aspx (http://msdn2.microsoft.com/en-us/office/default.aspx)

這篇文章中的資訊適用於:
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Access 2002 Standard Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft Word 2002 Standard Edition
  • Microsoft Office Access 2003
  • Microsoft Office Excel 2003
  • Microsoft Office PowerPoint 2003
  • Microsoft Office Word 2003
關鍵字:?
kbmt kbautomation kbhowto KB306682 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:306682? (http://support.microsoft.com/kb/306682/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。