简体   繁体   English

将参数从Outlook传递到Excel

[英]Passing parameters from Outlook to Excel

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

通过使用Application.Run

objExcel.Run "MacroName", param1, param2

You can execute a macro via the Application.Run method. 您可以通过Application.Run方法执行宏。 This method takes the macro name as the first argument and then up to 30 parameters that are passed as arguments to the macro. 此方法将宏名称作为第一个参数,然后最多将30个参数作为参数传递给宏。

In Outlook use the following code: 在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

In Excel, add the following method to the ThisWorkbook element of a spreadsheet document: 在Excel中,将以下方法添加到电子表格文档的ThisWorkbook元素中:

Option Explicit

Public Sub SayHello(message As String)

    MsgBox message

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM