Solved

Scheduled Task won't close Outlook

Posted on 2013-06-06
21
378 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
21 Comments
 
LVL 47

Expert Comment

by:apache09
Comment Utility
Rather than using Quit

Have you tried using Task Kill?

Taskkill.exe /f /im outlook.exe
0
 

Author Comment

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

Assisted Solution

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

Expert Comment

by:Ross Waddell
Comment Utility
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
Comment Utility
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 51

Accepted Solution

by:
Joe Winograd, EE MVE earned 500 total points
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
Very interesting!
0
 

Expert Comment

by:Ross Waddell
Comment Utility
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 51

Assisted Solution

by:Joe Winograd, EE MVE
Joe Winograd, EE MVE earned 500 total points
Comment Utility
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
Comment Utility
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 51

Expert Comment

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

Expert Comment

by:Ross Waddell
Comment Utility
No, I checked the box about not needing it and it seemed to work.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
Excellent!
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
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 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
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 51

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Resolve DNS query failed errors for Exchange
Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now