Gustav Brock
Gustav at cactus.dk
Thu May 26 04:22:17 CDT 2005
Hi Mark Here is how (as I learned from Mr. Colby). It works very reliably: Function RunExcelMacros( _ ByVal strFileName As String, _ ParamArray avarMacros()) As Boolean Debug.Print "xl ini", Time On Error GoTo Err_RunExcelMacros Static xlApp As Excel.Application Dim xlWkb As Excel.Workbook Dim varMacro As Variant Dim booSuccess As Boolean Dim booTerminate As Boolean If Len(strFileName) = 0 Then ' Excel shall be closed. booTerminate = True End If If xlApp Is Nothing Then If booTerminate = False Then Set xlApp = New Excel.Application End If ElseIf booTerminate = True Then xlApp.Quit Set xlApp = Nothing End If If booTerminate = False Then Set xlWkb = xlApp.Workbooks.Open(FileName:=strFileName, UpdateLinks:=0, ReadOnly:=True) ' Make Excel visible (for troubleshooting only) or not. xlApp.Visible = False 'True For Each varMacro In avarMacros() If Not Len(varMacro) = 0 Then Debug.Print "xl run", Time, varMacro booSuccess = xlApp.Run(varMacro) End If Next varMacro Else booSuccess = True End If RunExcelMacros = booSuccess Exit_RunExcelMacros: On Error Resume Next If booTerminate = False Then xlWkb.Close SaveChanges:=False Set xlWkb = Nothing End If Debug.Print "xl end", Time Exit Function Err_RunExcelMacros: Select Case Err Case 0 'insert Errors you wish to ignore here Resume Next Case Else 'All other errors will trap Beep MsgBox "Error: " & Err & ". " & Err.Description, vbCritical + vbOKOnly, "Error, macro " & varMacro Resume Exit_RunExcelMacros End Select End Function /gustav >>> MarkBoyd at McBeeAssociates.com 05/25 11:28 pm >>> I have a need to run Excel macro code from within Access VBA. Basically, the code logic flows as follows: 1. Export Access report as a new Excel file (DoCmd.OutputTo acOutputReport, strXLName, acFormatXLS, , True) 2. Run Excel macro (from within Access VBA) to format the Excel file accordingly. Does anyone have any sample code and/or resources related to this logic? Any help is greatly appreciated. Thanks, Mark Boyd I/S Supervisor McBee Associates, Inc.