Situation:
I am attempting to run a VBScript from windows command line. The script opens excel, imports a .bas
file and runs the macro within. The macro should then display a VBA MsgBox.
- If I right click on the
.vbs
file, on the desktop, and selectRun VBScript
, it works perfectly.
If I execute the following from the command line, in the correct location (i.e. desktop), no MsgBox is displayed though everything appears to run without error.
Test.vbs
What I have tried:
I have looked at lots of articles some of which I list as references. In particular, I worked my way through this answer. I had already tried things like:
CScript Test.vbs
WScript Test.vbs
CScript //logo Test.vbs
WScript //nologo Test.vbs
Following on from reading that answer, I then confirmed that Windows knew how to launch .vbs files, which seemed self-evident as I could right-click and run.
So:
1) Running assoc .vbs
gave me the expected:
:\Windows\System32>assoc .vbs
.vbs=VBSFile
2) Running ftype VBSFile
gave the expected:
VBSFile="%SystemRoot%\System32\WScript.exe" "%1" %*
3) I tried adding Pause and wait times to the command line string to no avail.
4) I have added various flags including debugging e.g. CScript //X
, no difference.
Question:
How do I run my VBScript from the command line and get the msgbox to appear?
VBSCript (Test.vbs)
Option Explicit
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Users\User\Desktop\Test.xlsb", 0, True)
xlApp.VBE.ActiveVBProject.VBComponents.Import "C:\Users\User\Desktop\Test.bas"
xlApp.DisplayAlerts = False
xlApp.Run "MessageMe"
xlApp.Wait Now + TimeSerial(0,0,2)
'xlApp.Quit
xlApp.DisplayAlerts = True
' Set xlBook = Nothing
' Set xlApp = Nothing
End Sub
Test.bas
Public Sub MessageMe
Msgbox "Hello"
End Sub
Notes:
All files reside on Desktop folder.
References:
Sub ExcelMacroExample()
in the script, seems that isn't full code? Try to add the lineMsgBox WScript.Path
to the script just to check the bitness and make sure that it doesn't affect execution. – omegastripesSub ExcelMacroExample()
is being launched. There is noExcelMacroExample
call within the code. And yes, putMsgBox WScript.Path
into the script, and check if the path is the same when you run VBS file via command line and right click. – omegastripes