PDA

View Full Version : Run a macro stored in Microsoft Excel



neodjandre
03-22-2009, 05:09 PM
Hello,

I would like some Outlook VBA code to execute a macro which is stored in a Microsoft Excel workbook from Microsoft Outlook 2007.

I know the equivalent in Excel VBA is:

Application.Run (wbname & "!procedurename)

but this does not work in Outlook VBA.

Can someone help please?

thanks
andy

JP2112
03-24-2009, 10:36 AM
Just create an instance of Excel, open the workbook and use the run method as you've described. Here's some air code:

Function runXLMacro(wbname As String, procedurename As String)
' wbname = file and path of workbook containing the macro you want to run
' procedurename = name of the Excel macro you want to run

Dim xlApp As Object
Dim xlwb As Object
Set xlApp = GetExcelApp
Set xlwb = xlApp.Workbooks.Open (wbname)

xlApp.Run (wbname & "!procedurename)

End Function

Function GetExcelApp() As Object
' always create new instance
On Error Resume Next
Set GetExcelApp = CreateObject("Excel.Application")
On Error GoTo 0
End Function



Hello,

I would like some Outlook VBA code to execute a macro which is stored in a Microsoft Excel workbook from Microsoft Outlook 2007.

I know the equivalent in Excel VBA is:

Application.Run (wbname & "!procedurename)

but this does not work in Outlook VBA.

Can someone help please?

thanks
andy

neodjandre
03-26-2009, 07:59 AM
ok this is helpful but the Excel Workbook is already open.

I don't want to create a new Excel instance.

If I do that then Macros will be automatically disabled and I get an error message that the macro cannot run because they are not enabled in Excel.

Your code needs to be modified so that it recognises the Excel instance already open!

thanks a lot again
andy