batch-filevbscriptscheduled-taskswshwindows-scripting

VBScript and batch file fails when run by Task Scheduler


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?


Solution

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