将参数从Outlook传递到Excel

时间:2010-08-04 19:38:37

标签: excel vba excel-vba outlook ms-office

如何将参数传递给从Outlook调用的Excel VBA代码?

2 个答案:

答案 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