2

I created a "Test" macro in Sheet1 of an excel sheet "Data" and trying to call it from vbs file

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\xxx\xxxx\xxxx\VB\Data.xlsm")

objExcel.Application.Run "C:\xxx\xxxx\xxxx\VB\Data.xlsm!Sheet1.Test" 
objExcel.ActiveWorkbook.Close

objExcel.Application.Quit

WScript.Quit

I am getting following error: The macro may not be available in this workbook or all macros may be disabled. Code: 800A03EC

I changed macro security to "Enable all macros" and also checked "Trust access to the VBA Project model" under Developer Macro settings. But still no luck

2 Answers 2

3

You can try it without Run, you can call your functions explicitly from workbook and sheets objects!

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\xxx\xxxx\xxxx\VB\Data.xlsm")
Set Sheet1 = objWorkbook.Sheets(1)
Sheet1.Test 
objExcel.ActiveWorkbook.Close

objExcel.Application.Quit

WScript.Quit
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent!! Its working now. Thank you. Also I found other way by using run. I posted that code also and accepting this as answer
@Ravi Adabala, Hey, cheers! I knew about your other way, but I just getting nervous when there's a work with multi-app projects thru Run.
1

Thanks Guys, I found solution for the above problem. May be problem was, I am trying to run a macro without opening excel workbook, not sure though. Anyhow this is the working code. I am able to run macros from vbs now

Option Explicit
Dim xlApp, xlBook

Set xlApp = CreateObject("Excel.Application")
set XlBook = xlApp.Workbooks.Open("C:\xxx\xxx\xxx\xxx\Data.xlsm")
xlApp.Run "Sheet1.Test"
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
set xlApp = Nothing
WScript.Echo "Finished"
WScript.Quit

1 Comment

you know you can run the MACRO without actually opening the Workbook, right? Which makes it faster

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.