VBScript and batch file fails when run by Task Scheduler

眉间皱痕 提交于 2019-12-25 02:06:42

问题


I have a VBScript that checks to see if MS Word is running hidden, makes it visible, then hides it again.

here is the script code that works fine when I double click the file in Explorer:

dim oWord
Dim wshShell, btn
Set wshShell = WScript.CreateObject("WScript.Shell")

set oWord = getobject(, "Word.Application")

if isobject(oWord) then 
    on error goto 0
    wshShell.Popup "Word is running, making visible", 7, "ALPS Push print", &H0 + &H40
    oWord.visible=true
    wshShell.Popup "MS Word is now visible" & vbcrlf & vbcrlf & "Waiting 30 seconds then hiding it", 30, "ALPS Push print", &H0 + &H30
    oWord.visible=false
else    
    wshShell.Popup "Word is not running" & vbcrlf & vbcrlf & "Quitting", 7, "ALPS Push print", &H0 + &H40
end if

It works find when I run it, but when it runs under Task Scheduler it fails so I created a batch file to launch it

wscript C:\dev\checkALPS.vbs

Now when I try to run it from the Task Scheduler, it still fails with the below error message

---------------------------
Windows Script Host
---------------------------
Script: C:\dev\checkALPS.bat
Line:   7
Char:   1
Error:  ActiveX component can't create object: 'getobject'
Code:   800A01AD
Source:     Microsoft VBScript runtime error

What can I do to get this working?


回答1:


I have had this similar issue, I bypassed it by utilizing cscript.exe application to activate the vbscript as a console application rather than a windows application. There is a possibility that there is a limitation on the domain or computer which does not allow windows applications to be executed via wscript. As an alternative, try activating the same script via "Cscript.exe" instead.

So the code would be:

cscript C:\dev\checkALPS.vbs

And the get object method is not activated off of the wscript executable. So you would need to activate it via wscript.

dim oWord
Dim wshShell, btn
Set wshShell = WScript.CreateObject("WScript.Shell")

set oWord = Wscript.GetObject(, "Word.Application")

if isobject(oWord) then 
    on error goto 0
    wshShell.Popup "Word is running, making visible", 7, "ALPS Push print", &H0 + &H40
    oWord.visible=true
    wshShell.Popup "MS Word is now visible" & vbcrlf & vbcrlf & "Waiting 30 seconds then hiding it", 30, "ALPS Push print", &H0 + &H30
    oWord.visible=false
else    
    wshShell.Popup "Word is not running" & vbcrlf & vbcrlf & "Quitting", 7, "ALPS Push print", &H0 + &H40
end if

Give that a swing and let me know how it works.



来源:https://stackoverflow.com/questions/21937151/vbscript-and-batch-file-fails-when-run-by-task-scheduler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!