任务计划程序运行时,VBScript和批处理文件失败

时间:2014-02-21 14:29:08

标签: batch-file vbscript scheduled-tasks wsh windows-scripting

我有一个VBScript,检查MS Word是否正在隐藏运行,使其可见,然后再次隐藏它。

这里的脚本代码在我在资源管理器中双击文件时工作正常:

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

当我运行它时可以找到它,但是当它在任务计划程序下运行时它会失败,所以我创建了一个批处理文件来启动它

wscript C:\dev\checkALPS.vbs

现在,当我尝试从任务计划程序运行它时,它仍然失败并显示以下错误消息

---------------------------
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

我能做些什么才能让它发挥作用?

1 个答案:

答案 0 :(得分:1)

我遇到过类似的问题,我通过利用cscript.exe应用程序来激活vbscript作为控制台应用程序而不是Windows应用程序来绕过它。域或计算机可能存在限制,不允许通过wscript执行Windows应用程序。作为替代方案,请尝试通过“Cscript.exe”激活相同的脚本。

所以代码是:

cscript C:\dev\checkALPS.vbs

并没有从wscript可执行文件激活get对象方法。所以你需要通过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

给它一个摆动,让我知道它是如何工作的。