0
votes

I need to call and execute VBA in an Excel spreadsheet from an Access form+button using VBA.

I have several Excel spreadsheets that are connected to my Access DB. I created a separate DB with VBA (C:\CDR & Project Inventory Reports\RebuildDB) to refresh and rebuild my source databases and report workbooks. I also created a separate workbook to refresh all of my other reports (C:\CDR & Project Inventory Reports\RefreshReports). Module 15 of that .xlsm will call all of my other modules to refresh those reports.

Prior to executing the Excel portion of the code is all of my code pertaining to rebuilding the DB. Currently, the Excel portion looks like this:

Dim oXLApp As Object
Set oXLApp = CreateObject("Excel.Application")
oXLApp.Visible = True
oXLApp.Workbooks.Open ("C:\CDR & Project Inventory Reports\RefreshReports.xlsm")
Debug.Print "Open report refresh code"
Debug.Print "refreshing all spreadsheets"

oXLApp.Run "RefreshReports.Module15" 'refreshes all CDR and Proj Inv spreadsheets

DoEvents
Debug.Print "Refresh completed"
oXLApp.ActiveWorkbook.Close (True)
oXLApp.Quit
Set oXLApp = Nothing
Debug.Print "Release object"

The problem I'm having is that oXLApp.Run doesn't actually execute the VBA it needs to. It calls it, then immediately closes. Is there any other way to call it that would result in the code actually running?

2
Your syntax is wrong rondebruin.nl/win/s9/win001.htmSierraOscar
Are you inside a resume next? if so remove it. Do you see the debug.prints? Note that write access to C:\ is restricted in modern versions of windows.Alex K.
Your line that calls the macro is only pointing to the Module, It needs to point to a specific Sub @MacroMan 's link is a good resource to correct thisEvan

2 Answers

0
votes
oXLApp.Run "RefreshReports.Module15"

should be

oXLApp.Run "RefreshReports.xlsm!NameOfMySub" '// Change sub name as required

As it stands, your code isn't running any VBA from Excel because you haven't referred to a specific sub. I presume you have error handling switched off as this should be rather noticeable.

0
votes

You should fully qualify the sub to be executed, by also giving the module name so

oXLApp.Run "RefreshReports.Module15"

should be

oXLApp.Run "'RefreshReports.xlsm'!Module15.NameOfMySub"

If you use

oXLApp.Run "'RefreshReports.xlsm'!NameOfMySub"

and the procedure NameOfMySub is declared in more than one module you will get and error message.

You could also re-write your code thus:

Dim oXLApp As Object
Dim Wkbk as workbook

Set oXLApp = CreateObject("Excel.Application")
oXLApp.Visible = True
set Wkbk  = oXLApp.Workbooks.Open ("C:\CDR & Project Inventory Reports\RefreshReports.xlsm")

Debug.Print "Open report refresh code"
Debug.Print "refreshing all spreadsheets"

oXLApp.Run "'" & Wkbk.Name & "'!Module15.NameOfMySub"

' Note use of single speechmarks arond workbook name