2

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.

  1. If I right click on the .vbs file, on the desktop, and select Run VBScript, it works perfectly.

Right click and run VBScript

  1. 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:

  1. Running Scripts from the Command Line
  2. VBS file doesn't run through cmd prompt
  3. Running Scripts from the Command Prompt
  4. How to run VBScript from command line without Cscript/Wscript
Community
  • 1
  • 1
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • How do you call `Sub ExcelMacroExample()` in the script, seems that isn't full code? Try to add the line `MsgBox WScript.Path` to the script just to check the bitness and make sure that it doesn't affect execution. – omegastripes May 07 '18 at 15:05
  • @omegastripes Forgive my ignorance in this field. That I am afraid is the whole script! I should be putting MsgBox WScript.Path into the VBScript? Seems wrong. So guess I have misunderstood. MsgBox WScript.Path in the Test.bas makes no difference. – QHarr May 07 '18 at 15:14
  • I simply want to command line execute Test.vbs > that opens Excel > imports Test.bas > Runs "MessageMe" macro that has now been imported > That generates a msgbox. – QHarr May 07 '18 at 15:15
  • 2
    I just mean that it is not clear how `Sub ExcelMacroExample()` is being launched. There is no `ExcelMacroExample` call within the code. And yes, put `MsgBox 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 May 07 '18 at 15:18
  • Ah so I think - know - this is my ignorance around VBScript. How would I add a line to make that call? And why does it run with right-click? No change with adding MsgBox WScript.Path to the .vbs inside the Sub. – QHarr May 07 '18 at 15:19
  • 1
    Hah...it works with the line to call added as simply ExcelMacroExample at the top. How stupid of me :-( @omegastripes Thank you. – QHarr May 07 '18 at 15:23

1 Answers1

5

In your main VBS, your code in MessageMe won't run. You'll need to include code to call your MessageMe Sub.

Also, in cscript, most people use instead of MsgBox, but apparently (new to me) MsgBox pops-up too...

WScript.Echo "From Main"

Call MessageMe

Public Sub MessageMe
    ' WScript.Echo "Hello"
    MsgBox "Hello"
End Sub
William Walseth
  • 2,803
  • 1
  • 23
  • 25
  • Maybe I am misunderstanding, if so I apologise, but I can right click and run and I get the vba msgbox displayed.This line xlApp.Run "MessageMe" should run the macro after it is imported to the excel project with xlApp.VBE.ActiveVBProject.VBComponents.Import "C:\Users\User\Desktop\Test.bas" – QHarr May 07 '18 at 13:16
  • If I do WScript.Echo I am not using the excel macro to produce the message box; which is my intention. – QHarr May 07 '18 at 13:16
  • Sorry I corrected my post, MsgBox does work from CScript (news to me). I think you just need something to call your ExcelMacroExample code from the 1st VBS script that your running from the command line. – William Walseth May 07 '18 at 13:20
  • I tried that. I should have added to my question. Sorry. Same result. – QHarr May 07 '18 at 13:21
  • I wonder if it's a permissions issue? Specifically creating the Excel object. You might wrap that CreateObject call in a try catch or step through it to see what's going on. – William Walseth May 07 '18 at 13:23
  • I'll give that a try but I think it imports the file fine as I have seen it added to the excel project. Just no message box :-( – QHarr May 07 '18 at 13:25