-->

vbscript to open an excel file in Windows 10 via T

2019-02-19 06:08发布

问题:

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.

回答1:

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.