2010-08-04 56 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