2

I have a script file that I'm executing via Task Scheduler that worked fine in Windows 7, and is not working in Windows 10.

Here's the code snippet:

Dim myxlApplication, myWorkBook

Set myxlApplication = CreateObject("Excel.Application")
myxlApplication.Visible = False
Set myWorkBook = myxlApplication.Workbooks.Open( emlAttach )
myxlApplication.DisplayAlerts = False
myWorkBook.Application.Run "Main.Main" 
myxlApplication.DisplayAlerts = True
myxlApplication.Quit
Set myxlApplication = Nothing

emlAttach is set earlier in the script to the absolute path and filename with extension.

When executing this via clicking on the script file, it works perfectly. When running it as a scheduled task, or forcing a run from task scheduler, it asks me what program I'd like to use to open the file. If I select Excel, it gives me an error telling me that the file doesn't exist.

The file extension on that error is wrong, xlsx vs what is qualified by the variable xlsb.

cgc007
  • 25
  • 1
  • 7
  • What is the command line? Are you explicitly using `wscript.exe myscript.vbs` or letting the .VBS extension implicitly note the overhead executable? (might have to be csript.exe for a console based executable) –  Aug 25 '15 at 04:53
  • Currently I'm just pointing it straight to the `.vbs` and expecting it to work. I just used cscript instead and that worked perfectly. Thank you very much for the help! If you'd like to write it up as a formal answer I can mark it as answered by you or I can write it up myself, whichever you prefer. =) – cgc007 Aug 25 '15 at 21:30
  • By the way, my original method, pointing to the vbs only, worked in Win7, but not in Win10 for some reason. I'm not sure what changed. – cgc007 Aug 27 '15 at 10:25
  • The Win7 box may have had the VBS file association with CSCRIPT rather than WSCRIPT. Or possibly Win7 handles window output differently from Win10 and the console version is not necessary; sorry I don't know enough to be more precise. –  Aug 27 '15 at 11:16

1 Answers1

6

Executing a .VBS from the Task Scheduler or as a login script (gpedit.msc ► user Configuration ► Windows Settings ► Scripts (Logon/Logoff)) will require explicit reference to the overhead executable. This would be WSCRIPT.EXE for a windows based return or CSCRIPT.EXE for a console based return. Further, if the .VBS is associated by default with WSCRIPT.EXE, you may need to specify CSCRIPT.EXE as the running process in order to avoid a windows based return.

CSCRIPT.EXE c:\<path to vbs script file>\myScript.vbs

Test the command line at a CMD prompt. You may need to enclose folder names in quotes if they contain spaces.