0
votes

Batch Code (option 1)

I typically used this to simply call vbs from batch, no variables included.

@echo off
myVBSpath=\\UNC\path
set vbsFileName=myVBS.vbs
cscript "%myVBSpath%\%vbsFileName%"
pause

Batch Code (option 2)

I typically use this option when passing a variable from batch to VBS... including the variable name & value after the vbs file path and name.

@echo off
set myVBSpath=\\UNC\path
set vbsFileName=myVBS.vbs
cscript //NoLogo "%myVBSpath%\%vbsFileName%" /attachment: "%myAttachment%"
pause

Resulting Batch Error Message

Regardless of which option I use above, I get the following error message:

\\UNC\path\myVBS.vbs(11, 1) Microsoft VBScript runtime error: Argument not optional

VBS Code

Dim xlApp 
Dim xlBook 
'Dim attachmentFullName
'attachmentFullName = WScript.Arguments.Named("attachment")

Set xlApp = CreateObject("Excel.Application") 
Set xlBook = xlApp.Workbooks.Open("\\UNCpath\myExcelFile.xlsm", 0, True) 

xlApp.DisplayAlerts = False
xlApp.Run "myMacro" ', Cstr(attachmentFullName)
xlApp.Quit 

Set xlBook = Nothing 
Set xlApp = Nothing 

Troubleshooting

I do not require a variable to be passed to vbs, so batch code (option 1) should be the way to go, and its how I started. When I got the runtime error message above, I tried to pass a false variable using option 2 just to see if that provided the extra argument the cscript command is looking for. Unfortunately, option 2 gave the same error message.

This is a fairly straight forward batch script, so I'm really at a loss and feel like maybe I've just been staring at it too long, making it harder than it needs to be, and I'm just missing something simple.

Would any of you be kind enough to give me a fresh set of eyes and give me an idea of what is causing my runtime error?

Thank you!

1
Did set in your batch file myAttachment as variable ?? i don't see it ??? - Hackoo
@Hackoo - I apologize, that was just a quick test solution, but yes, I just did set myAttachment=1, just so it had an argument variable to pass to vbs (and adjusted my vbs to accept an argument). - TMY
Can you edit your question and post the source code of the vbscript file ?? - Hackoo
The error to me looks like the running of myMacro. What's the code for that? Please post it up. It looks like it expects a parameter, so it is this line where you have the syntax incorrect. - Davy C
Winner, winner, chicken dinner -- @DavyC gets a gold star. I had commented out the use of the variable everywhere in my VBA sub... but in the sub itself I was still asking for an override variable from VBS... Sub myMacro(t as string) -- just deleted (t as string) and was back up and running. - TMY

1 Answers

0
votes

My solution was likely a one off - but here is a quick summary of troubleshooting steps for those noticing this error in their batch script:

  1. Check the cscript line to make sure no arguments are being accidentally passed (or not passed).
  2. Check VBS to ensure the code is no longer requesting an argument variable (or is accepting one if passed) -- thanks @Hackoo
  3. Check VBA to ensure the Sub is not requesting a variable -- thanks @DavyC!