2010-08-04 54 views

回答

3

通过使用Application.Run

objExcel.Run "MacroName", param1, param2 
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