一个调用 Excel 宏的例子:VB 窗体代码: Option Explicit Private Sub Command1_Click() Dim oExcelApp As Object ' Create a reference to the currently running excel application Set oExcelApp = GetObject(, "Excel.application") ' Make the Excel Application Visible. oExcelApp.Visible = True ' Run the excel procedure oExcelApp.run "proc", "David", 30 End SubExcel 中的宏: Sub Proc(sParam1 As String, iParam2 As Integer) MsgBox sParam1 & " is " & iParam2 & " Years Old" End Sub
选菜单“工具”—“宏”—“宏”,在弹出对话框中选中你录制的宏,单击“编辑”按钮。将代码复制出来,粘贴到你的 VB 代码中。当然你要在前后补上打开和关闭相关对象的代码。一般来说,稍加调试就可以使用。
Option Explicit Private Sub Command1_Click()
Dim oExcelApp As Object ' Create a reference to the currently running excel application
Set oExcelApp = GetObject(, "Excel.application")
' Make the Excel Application Visible.
oExcelApp.Visible = True
' Run the excel procedure
oExcelApp.run "proc", "David", 30
End SubExcel 中的宏:
Sub Proc(sParam1 As String, iParam2 As Integer)
MsgBox sParam1 & " is " & iParam2 & " Years Old"
End Sub