Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 627
  • Last Modified:

Scheduled Task won't close Outlook

I am running Windows7 64 bit and Outlook 2010

I have a batch file that produces a different result if I double click on it vs if I run it from Task Scheduler

I have at .bat file that has the following command:
cscript.exe CloseOutlook.vbs

The command is supposed to close any running Outlook Processes.
The contents of CloseOutlook.vbs:
Function IsProcessRunning( strServer, strProcess )
    Dim Process, strObject
    IsProcessRunning = False
    strObject   = "winmgmts://" & strServer
    For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
 If UCase( Process.name ) = UCase( strProcess ) Then
            IsProcessRunning = True
            Exit Function
        End If
    Next
End Function

On Error Resume Next

Dim strComputer, strProcess

   strProcess = "outlook.exe"
   strComputer = "."

If( IsProcessRunning( strComputer, strProcess ) = True ) Then

Set Outlook = GetObject(, "Outlook.Application")
If Err = 0 Then
Outlook.Quit()
End If 
Else
    WScript.Echo "Process " & strProcess & " is NOT running on computer " & strComputer
End If

Open in new window


If I double click on my batchfile it works perfect.  However if I schedule it in Task Scheduler it doesn't close outlook.  If no Outlook is running it it does give me the message, "Process outlook.exe is NOT running on  computer " But if Outlook is running it apparently finds it and does nothing.  

In Task Scheduler I have "Run with highest privileges" set.
I have tried running it "only when user is logged on" and "whether user is logged on or not"

The batch file has other commands and it runs to completion without error, but Outlook remains open.
0
mycomputerworks
Asked:
mycomputerworks
3 Solutions
 
apache09Commented:
Rather than using Quit

Have you tried using Task Kill?

Taskkill.exe /f /im outlook.exe
0
 
mycomputerworksAuthor Commented:
My understanding is that Taskkill.exe can cause outlook to go down in an ungraceful way and sometime break the .pst file.
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
Two comments: (1) You addressed the options in the General tab (Run with highest privileges, Run only when user is logged on, Run whether user is logged on or not), but maybe an option in the Conditions or Settings tab is causing the problem. I don't know what that might be, but it's worth some experimentation.

(2) I'm doing the same thing you are (exiting Outlook at a specified time)...and probably for the same reason (to backup the PST file). If you're open to another technique, I use a (free!) scripting/keyboard macro product called AutoHotkey:
http://www.autohotkey.com/

I use both Outlook and Thunderbird, and I have a batch file that the Task Scheduler runs daily (has never failed in several years of usage). The batch file contains this:

"c:\Program Files (x86)\AutoHotkey\AutoHotkey.exe" c:\bats\exitoutlooktbird.ahk

The AHK script contains this:
SetTitleMatchMode, 2
IfWinExist Microsoft Outlook
  {
   WinActivate
   Send !{F4}
  }
Sleep 3000
IfWinExist Mozilla Thunderbird
  {
   WinActivate
   Send !{F4}
  }

Open in new window


Not the most elegant solution, but it works via Task Scheduler every time. I'm sure you can write the same script in another language (like VBS or PowerShell) or even in the BAT file itself (not my area of deepest expertise), but I like AutoHotkey and tend to use it in situations like this. Regards, Joe
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ross WaddellCommented:
I'm stuck on this, too. I need to close Outlook every morning and while my .bat/.vbs script works fine from a command line (not even one with elevated permissions) I can't get it to work from Task Scheduler. It seems like the problem is with GetObject() - when running from a command prompt it returns a reference to Outlook but when executed via Task Scheduler it returns nothing.

Here's my .bat file:
@echo off
echo "==========================================" >> d:\outlook\batch_log.txt
echo %date% %time% >> d:\outlook\batch_log.txt
echo "Closing Outlook via vbs ..." >> d:\outlook\batch_log.txt
cscript //Nologo //B D:\Outlook\Close_Outlook.vbs >> d:\outlook\batch_log.txt 2>>D:\Outlook\errorlog.txt

Open in new window


Here's my .vbs file:
Function IsProcessRunning( strServer, strProcess )
    Dim Process, strObject
    IsProcessRunning = False
    strObject = "winmgmts://" & strServer
    For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
        'objFile.WriteLine Process.name
        If UCase( Process.name ) = UCase( strProcess ) Then
            IsProcessRunning = True
            Exit Function
        End If
    Next
End Function

on error resume next

Dim oOL 'As Outlook.Application
Dim objFSO 'As FileSystemObject
Dim objFile 'As File
Dim strComputer, strProcess

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("D:\Outlook\vbs_log.txt", True)

objFile.WriteLine "VBS: " & Now

strProcess = "outlook.exe"
strComputer = "."

If( IsProcessRunning( strComputer, strProcess ) = True ) Then
  objFile.WriteLine "IsProcessRunning=TRUE"

  Set oOL = GetObject(, "Outlook.Application")
  'Set oOL = CreateObject("Outlook.Application")

'WScript.Sleep 5000

  If oOL Is Nothing Then
    'no need to do anything, Outlook is not running
    objFile.WriteLine "GetObject() result is nothing"
  Else
    'Outlook running
    objFile.WriteLine "GetObject() result is NOT nothing, attempting to log off from session and quit"
    If Err = 0 Then
      oOL.Session.Logoff
      oOL.Quit
    End If
  End If
Else
  objFile.WriteLine "IsProcessRunning=FALSE"
End If

objFile.Close

Set oOL = Nothing
Set objFile = Nothing
Set objFSO = Nothing

Open in new window


Here's the output in vbs_log.txt when run from command prompt:
VBS: 07/30/2014 2:34:51 PM
IsProcessRunning=TRUE
GetObject() result is NOT nothing, attempting to log off from session and quit

Open in new window


Here's the output in vbs_log.txt when executed from Task Scheduler:
VBS: 07/30/2014 2:33:44 PM
IsProcessRunning=TRUE
GetObject() result is nothing

Open in new window


Why doesn't the GetObject() call work from Task Scheduler? I have the task set up to 'Run only when user is logged on', 'Run with highest privileges' and the Start In is set to the same folder as the .bat & .vbs files.
0
 
Ross WaddellCommented:
I added some error handling to the .vbs script and ONLY when I run this from Task Scheduler do I see this:

Error 429: ActiveX component can't create object

Open in new window

0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
Hi Ross,
I run the BAT file with the AutoHotkey script mentioned in my earlier post <http:#a39230433> with the same Task Scheduler settings that you're using — "Run with highest privileges" and "Run only when user is logged on". It has run perfectly every day for several years — has never failed to close both Outlook and Thunderbird. If you're adventurous, give it a spin. There's been a new fork of the AutoHotkey language since my last post:

http://ahkscript.org

The download and install should take just a minute or two. Create the BAT file and AHK file, and you'll know in five minutes if it works for you. Regards, Joe

P.S. I see that the question says "Abandoned (14 Days)". It should really say "Abandoned (14 Months)".   :)
0
 
Ross WaddellCommented:
Thanks Joe. It works perfectly although I'd rather use vbscript. Until I can figure out what's wrong with GetObject(), though, I'll use your solution.
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
Ross,
I'm glad to hear that it works perfectly for you. Strange that an AHK script works fine in a BAT file called via TS but a VBScript does not. Please post back here when you figure out the problem. I'm curious, although the original asker has obviously lost interest. Regards, Joe
0
 
Ross WaddellCommented:
The same .vbs script works on a colleague's machine so perhaps it's a problem with the MS Office install on my box.
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
Very interesting!
0
 
Ross WaddellCommented:
While the AHK script works perfectly when I'm logged on and working on the desktop, it doesn't seem to work when I lock it at night (still logged in, though).
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
Ross,
I'm not 100% sure on this, but it's probably because when the computer is locked, it won't accept any input. But the AHK script is sending input — in essence, "clicking" on the Outlook title bar to make it the focus window and then "hitting" the Alt-F4 key on the keyboard. My guess is that the locked status won't allow that "input". You could try changing the TS task to "Run whether user is logged on or not", which may or may not need the password to be stored (I don't know). I've never used TS with that setting, so I don't know if it will work, but it's worth a shot. Please let me know how it goes. Regards, Joe
0
 
Ross WaddellCommented:
That worked! Thanks Joe. I hadn't considered changing this setting as my research showed it needed to be 'Run only when users is logged on' for the aborted .vbs attempt (which is also true for my colleague who was able to use the .vbs version).
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
That's great news, Ross. Did you have to store the password?
0
 
Ross WaddellCommented:
No, I checked the box about not needing it and it seemed to work.
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
Excellent!
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
Hi Ross,
I'm trying to clean up some open questions and noticed that I haven't heard from you in nearly three months on this one. Back on 1-Aug, it seemed that everything was resolved, so I'd appreciate it if you close out the question. However, if there are still open issues, please let me know and I'll try to continue helping. Thanks, Joe
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
I recommend that post https:#a39230433 be the Accepted Solution and post https:#a40230116 be an Assisted Solution. After I made those posts with the AutoHotkey (AHK) script and instructions on calling it from Task Scheduler, the asker said, "Thanks Joe. It works perfectly although I'd rather use vbscript. Until I can figure out what's wrong with GetObject(), though, I'll use your solution."

He apparently continued to use my solution, because in a subsequent post he said, "While the AHK script works perfectly when I'm logged on and working on the desktop, it doesn't seem to work when I lock it at night (still logged in, though)."

I solved that problem for him in my post https:#a40234244 (which could also be an Assisted Solution), after which he said, "That worked! Thanks Joe."

We exchanged a few posts after that and then he abandoned the question, never closing it. Regards, Joe
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
Thank you, Mr. Wolfe — I appreciate it!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now