VBA & Macro Problems

Run a Macro in an Open Excel Workbook with VBScript

Steve KSteve K Nov 18, 2026 869 views

Question details

Run a VBA macro in an Excel workbook that may already be open, using VBScript.

How to Run a Macro in an Open Excel Workbook with 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.
Before you start

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.

Solution 1Recommended

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.

1
Create the script

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"

2
Set the path and macro name

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.

3
Run the VBScript

Save the file with a .vbs extension and run it. The script leaves Excel and the workbook open after calling the macro.

Attach to Excel and run the workbook's macro
Check the Excel instance and macro security: If multiple Excel instances are running, the attached instance may not contain the workbook you expect. Excel may also block macros from untrusted files; review the security warning and enable macros only if you trust the workbook.
Free Microsoft Office alternative

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.

Open and edit Excel spreadsheet formats such as XLSX and XLSMUse a familiar spreadsheet interface for everyday workbook tasksMove spreadsheet work with a lightweight office suiteReview macro compatibility before moving an automated workbook
microsoft office alternative - wps office

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.