2014-02-21 93 views
0

我有一个VBScript,检查MS Word是否正在运行隐藏,使其可见,然后再次隐藏它。由任务计划程序运行VBScript和批处理文件失败

这里是脚本代码,当我双击在资源管理器中工作正常:

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

您的任务是否以有限权限运行?你在64位操作系统上?如果是这样,请确保您在任务/ bat中使用的WSCRIPT.EXE是您想要的(\ System32 for 64-bit或\ SysWOW64 for 32-bit)。 – Bond

+1

适合我。我怀疑计划任务的配置有问题。你在任务的安全选项中配置了什么?该错误消息表明,在任务正在运行的用户的上下文中没有运行Word进程。 –

+0

该任务设置为在Admin帐户下以最高权限运行。 Word实例由在同一个帐户下运行的.NET应用程序创建。 –

回答

1

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

因此,代码是:

cscript C:\dev\checkALPS.vbs 

的GET对象的方法不脱WScript的可执行文件的启动。所以你需要通过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 

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

+0

如何让它在没有窗口的情况下运行? –

+0

@oO。查看http://stackoverflow.com/tags/vbscript/info部分。具体在“本地执行脚本”下。 – Rich

相关问题