Sunday, July 6, 2008

Close Outlook gently with vBscript

I needed a small script to run at log off or shutdown that would close Outlook gently. As sometimes a few of the users I work with forget to close it before logging off or shutting down. The point is to avoid corrupting Outlook profile or the PST it may have open.

This was tested with Vista, Windows XP, Office 2007 and Office 2003.

I pieced this quickly together from other sources on the internet.
Save the below code as a .vbs file.



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



To apply this script so it runs on shutdown and/ or logoff do the following,

Goto Start > Run
In Run window, type gpedit.msc, hit enter.
In the GPEDIT window, under User Configuration,
Goto Windows Settings
Goto Scripts (Logon/Logoff)
Goto Logoff
Choose add and browse to your recently created .vbs script.
Hit apply.

This could also work from a Domain applied group policy object.

I also use it on a server to shutdown outlook that I have setup running as a service. I applied the script as a computer shutdown script.

No comments: