Run a Macro in an Open Excel Workbook with VBScript
Question details
Run a VBA macro in an Excel workbook that may already be open, using VBScript.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A VBScript needs to use an existing Excel session instead of always starting a new one.
- Observed behavior
- CreateObject starts a new Excel instance, while the user wants to run a macro in an already open workbook.
Identify the workbook's full path and the name of a public macro in a standard VBA module. Confirm that the workbook is macro-enabled and that Excel's security settings allow its macros to run.
Attach to Excel and run the workbook's macro
Use GetObject to attach to an available Excel instance, and create one only if none is available.
The script searches the attached instance for the workbook by full path. If it is not found there, the script opens it before calling the macro. GetObject may attach to a different instance if multiple Excel instances are running.
Open a text editor and paste the following VBScript: Dim objExcel, wb, item, workbookPath workbookPath = "C:\Path\File.xlsm" Set objExcel = Nothing On Error Resume Next Set objExcel = GetObject(, "Excel.Application") Err.Clear On Error GoTo 0 If objExcel Is Nothing Then Set objExcel = CreateObject("Excel.Application") End If objExcel.Visible = True Set wb = Nothing For Each item In objExcel.Workbooks If StrComp(item.FullName, workbookPath, vbTextCompare) = 0 Then Set wb = item Exit For End If Next If wb Is Nothing Then Set wb = objExcel.Workbooks.Open(workbookPath) End If objExcel.Run "'" & Replace(wb.Name, "'", "''") & "'!Module1.MacroName"
Replace C:\Path\File.xlsm with the workbook's full path, and replace Module1.MacroName with the standard module and public macro name shown in Excel's VBA editor.
Save the file with a .vbs extension and run it. The script leaves Excel and the workbook open after calling the macro.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel formats, making it easy to move everyday spreadsheet work. It does not fix Microsoft Excel's VBScript automation behavior; check macro compatibility before migrating automated workbooks.

Frequently Asked Questions
Why does CreateObject open another Excel instance?
CreateObject("Excel.Application") creates a new application instance. Try GetObject(, "Excel.Application") first to attach to an available running instance.
How do I specify which workbook contains the macro?
Use a workbook-qualified name with Excel's Run method, such as 'File.xlsm'!Module1.MacroName. The script above builds the qualifier from the workbook it finds or opens.
What if the workbook is open in another Excel instance?
GetObject may attach to an instance that does not contain the workbook. Check the running Excel instances before using the script, because searching objExcel.Workbooks only checks the attached instance.
Why might Excel refuse to run the macro?
The macro name or workbook reference may be incorrect, or Excel's security settings may block the macro. Check the name in the VBA editor and review any Excel security warning; enable macros only for a trusted file.




