PDA

View Full Version : Excel Macros from Access?


Larry Hamilton
01-28-2004, 09:45 AM
Must be something in the water out there. Lately, I have received 3 emails about how do you run an Excel Macro from Access. Getting tired of responding, so here it is:

I call it "DoIt", sorry, when I am really cooking, the creative-naming part of my brain shuts down

===========================================

'I take two variables with me to this sub, you can take
'as many or as few as you like

Sub DoIt7(resultsinput As String, targetday As String)
Dim objAppExcel As Excel.Application

On Error Resume Next 'Defer error checking for Excel application check
'If Excel is running then link to it
Set objAppExcel = GetObject(, "Excel.Application")
'If Excel is not running then run it
If objAppExcel Is Nothing Then
Set objAppExcel = CreateObject("Excel.Application")
End If
On Error GoTo ErrorHandler 'Resume error handling
'Make Excel visible if necessary.
objAppExcel.Visible = False
'Open workbook with macro to run
objAppExcel.Workbooks.Open "D:MyDocs\zzMacros.xls"

'Run macro in Excel

objAppExcel.Run "Macro7", targetday


objAppExcel.Quit
Exit Sub
ErrorHandler:
MsgBox Err.Source & " Error: " & Err.Number & vbCrLf & Err.Description
End Sub


enjoy