摘要
从 Visual Basic 自动执行 Office 产品时,将部分代码移动到可在服务器的进程空间中运行的 Microsoft Visual Basic for Applications (VBA) 模块可能很有用。 如果服务器仅在进程内进行调用时执行一项操作,这可提升应用程序的整体执行速度,并帮助缓解问题。
本文演示如何从 Visual Basic 将 VBA 模块动态添加到正在运行的 Office 应用程序,然后调用宏以填充进程内工作表。
更多信息
以下示例演示了将代码模块插入到 Microsoft Excel,但可以针对 Word 和 PowerPoint使用相同的技术,因为两者均包含相同的 VBA 引擎。
该示例将静态文本文件用于插入 Excel 中的代码模块。 可能需要考虑将代码移动到可以编译到应用程序中的资源文件中,然后根据需要,在运行时提取到临时文件中。 这样,项目就更易于管理,可以重新分发。
从 Microsoft Office XP 开始,用户必须先授予对 VBA 对象模型的访问权限,然后才能运行编写用于操作 VBA 的任何自动化代码。 这是使用 Office XP 的新安全功能。 有关详细信息,请参阅以下知识库文章:
282830 对 XP VBA Office编程Project访问被拒绝
生成示例的步骤
-
首先,创建名为 KbTest.bas 的新文本文件 (,.txt扩展) 。 这是我们将运行时插入到Excel的代码模块。
-
在文本文件中,添加以下代码行:
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 -
将文本文件保存到 C:\KbTest.bas 目录,然后关闭该文件。
-
启动Visual Basic并创建标准项目。 默认情况下创建 Form1。
-
在"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 库。
-
-
将按钮添加到 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 -
对于 Excel 2002 和更高版本的 Excel,必须打开访问 VBA 项目。 为此,请使用下列方法之一:
-
在 Excel 2007 中,单击"Microsoft Office按钮",然后单击"Excel选项"。 单击"信任中心",然后单击"信任中心设置"。 单击"设置",单击以选中"信任访问 VBA 项目对象模型"复选框,并单击"确定"两次。
-
在 Excel 2003 和早期版本的 Excel 中,指向"工具"菜单上的"宏",然后单击"安全性"。 在"安全性"对话框中,单击"受信任的源"选项卡,然后单击以选中"信任对Visual Basic Project访问"复选框。
-
-
运行Visual Basic项目。
参考
有关从 Office 自动化Visual Basic,请参阅以下Office开发支持站点: