Solved

Scheduled Task won't close Outlook

Posted on 2013-06-06
21
481 Views
Last Modified: 2016-07-06
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
Comment
Question by:mycomputerworks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
21 Comments
 
LVL 47

Expert Comment

by:apache09
ID: 39228026
Rather than using Quit

Have you tried using Task Kill?

Taskkill.exe /f /im outlook.exe
0
 

Author Comment

by:mycomputerworks
ID: 39229160
My understanding is that Taskkill.exe can cause outlook to go down in an ungraceful way and sometime break the .pst file.
0
 
LVL 54

Assisted Solution

by:Joe Winograd, EE MVE
Joe Winograd, EE MVE earned 500 total points
ID: 39230433
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Expert Comment

by:Ross Waddell
ID: 40229944
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
 

Expert Comment

by:Ross Waddell
ID: 40230054
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
 
LVL 54

Accepted Solution

by:
Joe Winograd, EE MVE earned 500 total points
ID: 40230116
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
 

Expert Comment

by:Ross Waddell
ID: 40230221
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
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 40230260
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
 

Expert Comment

by:Ross Waddell
ID: 40230271
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
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 40230305
Very interesting!
0
 

Expert Comment

by:Ross Waddell
ID: 40234140
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
 
LVL 54

Assisted Solution

by:Joe Winograd, EE MVE
Joe Winograd, EE MVE earned 500 total points
ID: 40234244
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
 

Expert Comment

by:Ross Waddell
ID: 40234372
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
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 40234418
That's great news, Ross. Did you have to store the password?
0
 

Expert Comment

by:Ross Waddell
ID: 40234429
No, I checked the box about not needing it and it seemed to work.
0
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 40234438
Excellent!
0
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 40411938
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
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 41692244
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
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 41694790
Thank you, Mr. Wolfe — I appreciate it!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you don't know how to downgrade, my instructions below should be helpful.
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question