Closing Outlook Programmatically

S

Scott

Hi,

I need to be able to close Outlook programmatically. This is to go in a
scheduled job that backs up the .pst file to a network drive. If the user
forgets to shut down Outlook, the backup fails.

pskill from sysinternals.com is a bit too harsh, as it kills the process
rather than closes the window.

And I would use psshutdown from sysinternals, but Outlook is not being well
behaved. If I try to shutdown the machine, I get a dialogue about closing
Outlook first. Why? What's so special about Outlook as opposed to say,
Word, Excel, Notepad, or just about any other application? If the shutdown
process sends a close command to all of those applications, they close. But
Outlook demands that you close it "manually".

I'm hoping I can shutdown Outlook (or any abitrary window) with a wscript???
But, if so, can you provide an example, as I don't know that scripting
language.

Thanks,
Scott
 
K

Ken Slovak - [MVP - Outlook]

Outlook requires a proper shutdown of the MAPI spooler to disconnect from
its data store. That's the case whether it's the external spooler in Outlook
2000 or earlier or the internal one for Outlook 2002 or later.

Use the following code, which is VBScript compatible:

Dim oOL 'As Outlook.Application
Set oOL = GetObject(, "Outlook.Application")
If oOL Is Nothing
'no need to do anything, Outlook is not running
Else
'Outlook running
oOL.Session.Logoff
oOL.Quit
End If
Set oOL = Nothing
 
M

Mark L. Ferguson

--close_outlook.vbs--
set wmi = GetObject("winmgmts:")
wql = "select * from win32_process where name='outlook.exe'"
set result = wmi.ExecQuery(wql)
for each instance in result
instance.Terminate(0)
next
--end file cut here--

or from command line, in XP, force close all running tasks, and shutdown

shutdown -f -s -t 0

--
Mark L. Ferguson MVP
Email address : (e-mail address removed) Subject: "QZ" + anything
All email without "QZ" in the subject line will be automatically deleted.
marfer's notes for XP > http://www.geocities.com/marfer_mvp/xp_notes.htm
..
 
S

Scott

Hi Ken,

Thanks for this. Much appreciated :)

There was a minor error in the code:

C:\Temp>cscript close_outlook.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\Temp\close_outlook.vbs(3, 18) Microsoft VBScript compilation error:
Expected 'Then'

But changing the code as follows closes Outlook:

Dim oOL 'As Outlook.Application
Set oOL = GetObject(, "Outlook.Application")
If oOL Is Nothing Then
'no need to do anything, Outlook is not running
Else
'Outlook running
oOL.Session.Logoff
oOL.Quit
End If
Set oOL = Nothing

However, if Outlook is not running, I get this error:

C:\Temp>cscript close_outlook.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\Temp\close_outlook.vbs(2, 1) Microsoft VBScript runtime error: ActiveX
component can't create object: 'GetObject'

Also, is there a way to genericize this script for any open application? Or
code to loop thru all open applications and close them?

Sorry for the questions - I'll hit the bookstore for a reference on VBScript
asap.

Thanks again,
Scott
 
S

Scott

Hi Mark,

Thanks for this. However, do you think instance.Terminate(0) is a bit
harsh? I had considered using pskill from sysinternals.com, but decided
against that. If pskill supported a Close or Quit instead of "kill -9", I
would have used it.

Can I change that to instance.Quit(0) instead???

Cheers,
Scott
 
K

Ken Slovak - [MVP - Outlook]

Sorry, I forgot the Then.

If you can't use GetObject for some reason then you can use
CreateObject("Outlook.Application") instead, but that will start Outlook if
it's not already running.

I know of no way to make something like that generic. You can use Win32 API
calls to enumerate all running applications, processes and existing windows
but that is a major job, can't be done from scripting which doesn't support
Win32 API calls, and still would have to decide what is a valid app to shut
down and what has to keep running. You can also issue Win32 API calls to
shut down all running apps, but as you've seen that isn't good with Outlook.
 
Joined
Aug 28, 2011
Messages
1
Reaction score
0
Hi All,
Can you tell me how can i detect count open mail numbers of outlook for Guest account from admin account?

Help would be really appreciated. Waiting for your eply.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top