Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

摘要

從 Visual Basic 自動化 Office 產品時,將部分程式碼移至可在伺服器程式空間中執行之 Microsoft Visual Basic for Applications (VBA) 模組可能很有用。 如果伺服器只在進行中通話時執行動作,這可提升應用程式的整體執行速度,並有助於減輕問題。

本文示範如何從 Visual Basic 動態新增 VBA 模組至Office應用程式,然後撥打宏以填入進行中的工作表。

其他相關資訊

下列範例示範將程式碼模組插入 Microsoft Excel,但您可以針對 Word 和 PowerPoint使用相同的 VBA 引擎。

範例使用靜態文字檔作為插入至Excel。 您可能會想要考慮將程式碼移動到資源檔案中,以編譯至您的應用程式,然後于執行時需要時解壓縮成臨時檔案。 這會使專案更易於重新發佈。

從 XP Microsoft Office開始,使用者必須先授予 VBA 物件模型存取權,才能使用任何撰寫以操作 VBA 的自動化程式碼。 這是使用 XP 的Office功能。 如需詳細資訊,請參閱下列知識庫文章:

282830 XP VBA Office程式存取Project拒絕

建立範例的步驟

  1. 首先,建立名為 KbTest.bas (副檔名.txt副檔名) 。 這是我們會于Excel插入程式碼模組。

  2. 在文字檔中,新增下列程式碼:

       Attribute VB_Name = "KbTest"

    ' Your Microsoft Visual Basic for Applications macro function takes 1
    ' parameter, the sheet object that you are going to fill.

    Public Sub DoKbTest(oSheetToFill As Object)
    Dim i As Integer, j As Integer
    Dim sMsg As String
    For i = 1 To 100
    For j = 1 To 10

    sMsg = "Cell(" & Str(i) & "," & Str(j) & ")"
    oSheetToFill.Cells(i, j).Value = sMsg
    Next j
    Next i
    End Sub
  3. 將文字檔儲存到 C:\KbTest.bas 目錄,然後關閉檔案。

  4. 開始Visual Basic建立標準專案。 表單 1 預設為建立。

  5. [Project功能表上,按一下 [參照,然後選取適當的型別程式庫版本,讓您使用早期裝訂Excel。

    例如,選取下列其中一項:

    • 針對 Microsoft Office Excel 2007,選取 12.0 文件庫。

    • 針對 Microsoft Office Excel 2003,選取 11.0 文件庫。

    • 針對 Microsoft Excel 2002,選取 10.0 文件庫。

    • 針對 Microsoft Excel 2000,選取 9.0 文件庫。

    • 針對 Microsoft Excel 97,選取 8.0 文件庫。

  6. 新增按鈕至 Form1,然後針對按鈕的 Click 事件將下列程式碼放在處理常式中:

       Private Sub Command1_Click()
    Dim oXL As Excel.Application
    Dim oBook As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    Dim i As Integer, j As Integer
    Dim sMsg As String

    ' Create a new instance of Excel and make it visible.
    Set oXL = CreateObject("Excel.Application")
    oXL.Visible = True

    ' Add a new workbook and set a reference to Sheet1.
    Set oBook = oXL.Workbooks.Add
    Set oSheet = oBook.Sheets(1)

    ' Demo standard Automation from out-of-process,
    ' this routine simply fills in values of cells.
    sMsg = "Fill the sheet from out-of-process"
    MsgBox sMsg, vbInformation Or vbMsgBoxSetForeground

    For i = 1 To 100
    For j = 1 To 10
    sMsg = "Cell(" & Str(i) & "," & Str(j) & ")"
    oSheet.Cells(i, j).Value = sMsg
    Next j
    Next i

    ' You're done with the first test, now switch sheets
    ' and run the same routine via an inserted Microsoft Visual Basic
    ' for Applications macro.
    MsgBox "Done.", vbMsgBoxSetForeground
    Set oSheet = oBook.Sheets.Add
    oSheet.Activate

    sMsg = "Fill the sheet from in-process"
    MsgBox sMsg, vbInformation Or vbMsgBoxSetForeground

    ' The Import method lets you add modules to VBA at
    ' run time. Change the file path to match the location
    ' of the text file you created in step 3.
    oXL.VBE.ActiveVBProject.VBComponents.Import "C:\KbTest.bas"

    ' Now run the macro, passing oSheet as the first parameter
    oXL.Run "DoKbTest", oSheet

    ' You're done with the second test
    MsgBox "Done.", vbMsgBoxSetForeground

    ' Turn instance of Excel over to end user and release
    ' any outstanding object references.
    oXL.UserControl = True
    Set oSheet = Nothing
    Set oBook = Nothing
    Set oXL = Nothing

    End Sub
  7. 針對 Excel 2002 和較新版本的 Excel,您必須開啟存取 VBA 專案。 若要解決此問題,請使用下列其中一種方法:

    • 在 Excel 2007 中,按一下 [Microsoft Office按鈕,然後按一下 [Excel選項。 按一下[信任中心,然後按一下信任中心設定。 按一下[宏設定,按一下以選取[信任存取至 VBA專案物件模型> 核取方塊,然後按一下 [確定兩次。

    • 在 Excel 2003 和較舊版本的 Excel 中,指向 [工具Excel 功能表上的 [宏,然後按一下 [安全性> 。 在 [安全性」對話方塊中,按一下 [信任的來源Visual Basic Project核取方塊。

  8. 執行Visual Basic專案。

參考

如需從 Office 自動化Visual Basic,請參閱Office開發支援網站:

HTTP://support.microsoft.com/ofd

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×