如何将参数传递给从Outlook调用的Excel VBA代码?
答案 0 :(得分:3)
使用Application.Run
:
objExcel.Run "MacroName", param1, param2
答案 1 :(得分:3)
您可以通过Application.Run
方法执行宏。此方法将宏名称作为第一个参数,然后将最多30个参数作为参数传递给宏。
在Outlook中使用以下代码:
Public Sub RunExcelMacro()
Dim excelApp As Object
Set excelApp = CreateObject("Excel.Application")
excelApp.Visible = True
' open the workbook that contains the macro
' or place the macro in a workbook in your XLSTARTUP folder
excelApp.Workbooks.Open "C:\tmp\book.xls"
' run the macro
excelApp.Run "ThisWorkbook.SayHello", "Hello World!"
excelApp.Quit
Set excelApp = Nothing
End Sub
在Excel中,将以下方法添加到电子表格文档的ThisWorkbook
元素中:
Option Explicit
Public Sub SayHello(message As String)
MsgBox message
End Sub