Is Outlook Running

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am programming an Access application and need to know how to determine
within VBA if Outlook is running. Yes, I've posted this message in the
Access group as well.
 
Hi,

If GetObject(,"Outlook.Application") Is Nothing Then
' OL isn´t running
Endif
 
I get an Runtime Error 429 "ActiveX Component cannot create object" when I
execute this statement.
 
It's a management library for windows systems. It gives information and control for nearly every aspect of windows. It can be accessed from nearly any scripting or programming language.

For what you are doing Michaels answer may be the easiest way to get your information. In one line it will either get the running instance of Outlook or it will let you know Outlook is not running. You have to catch the error to know that the application is NOT running. You also have to use a different name for the object when asking from outside of the app I believe.

I am trying to test this right now. Unfortunanately I am remoting in from an old laptop that is 800/600 and my workstation is 1280X. It's pretty uncomfortable but I am stuck out here until tomorrow afternoon. Everything on the screen is scrunched into a windows that iss one quarter of the sive I left my desktop at.

Here is a sample of WMI code in Access VBA
Dim colProcesses As Object
Dim objProcess As Object
Set objWMIService = GetObject("winmgmts:")
Set colProcesses = objWMIService.ExecQuery("select * from win32_process")

Search the MS libraries for more on the syntax and usage of the select statement.

The beauty of WMI (Windows Management Instrumentation) is that we can say things like "select name from win32_process where name like "outlook*" and get an answer without throwing exceptions which makes it good for scripting.

GetObject will return the instance reference for an existing object if you now it's OLE or COM string ID. In the WMI example we use the moniker for WMI which is "winmgmts:".

More beyond this is beyond me right now but you might look into it if you are a sysadmin ot enterprise developer.



So to finish what Michael forgot to add - (I make the same mistake all of the time)

Sub testit()
Dim o As Object
Set o = SafeGetObject("Outlook.Application")
If o Is Nothing Then
MsgBox "Outlook not running"
Else
MsgBox "Outlook is running"
End If
Set o = Nothing ' you have to release it or Outlook won't close.
End Sub

Function SafeGetObject(strClassName As String) As Object
On Error GoTo error_exit

Set SafeGetObject = GetObject(, strClassName)
GoTo exit_function

error_exit:
Set SafeGetObject = Nothing

exit_function:

End Function


We have to guard against the null object so when the assignment fails we just ignore it because the return value of our function WILL be Nothing and so is testable. You can do many variations on this. This one returns the object we are looking for or Nothing. You could return True/False if you only need that. Your choice.

Be sure to release the reference "o" in this case so the Outlook application can be exited if the user wants to otherwise you will have to handle the users issues when you are attached. Outlook security won't let you do much with an already running app without extra steps.
 
Hi Roy,

for catching an error you would need to add your own error handler. In
this case you could just add a "On Error Resume Next" statement before
calling GetObject.
 
Also - see the following below are links to WMI info for dotNET.
http://msdn.microsoft.com/library/d...en-us/smssdk03/hh/sms/usingsms_csharpNode.asp
SMS and the .Net Managment Classes
The .NET management classes (System.Managment) provide managed code access to WMI. As users of WMI this enables SMS 2003, or SMS 2.0, to be accessed from any language capable of using the .NET framework. For example, C#, C++ and Visual Basic .NET.

The .NET Management classes clean up a lot of the idiosyncrasies of the WMI object model to present its own, consistent, object oriented, model. The basic WMI tasks of WMI connection, object enumeration, method execution, object deletion, object retrieval and event capture can all be achieved through the .NET Management classes.

This section present samples for all these tasks. There is however several different .NET Management classes for manipulating WMI and you should consult the Platform SDK for complete information. The samples are presented in C# but will readily translate to other .NET languages.

Further information on the .NET Management classes can be found on MSDN at Managing Applications Using WMI.

This section documents the following tasks:

a.. Connecting to WMI
b.. Retrieving a WMI Object
c.. Enumerating WMI Objects
d.. Creating a WMI Object
e.. Deleting a WMI Object
f.. Executing a WMI Class Method
g.. Capturing a WMI event
 
' Try this in Access
' Edited from ScriptoMatic 2.0
' added WHERE clause to look for "Outlook.exe"
' safely returns the Outlook run state without disturbing the application
' It will also tell you about any executable even if it is in the midst of
startup or shutdown
' Many other valuable items are returned - many more than GetObject
' the process ID will let you get all of the windows and control the process
with the WIndows API.
Sub getOutlook() as Boolean
On Error Resume Next

Const wbemFlagReturnImmediately = &H10
Const wbemFlagForwardOnly = &H20

arrComputers = Array(".")
For Each strComputer In arrComputers
Debug.Print
Debug.Print "=========================================="
Debug.Print "Computer: " & strComputer
Debug.Print "=========================================="

Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process where
caption = 'Outlook.exe'", "WQL", _
wbemFlagReturnImmediately +
wbemFlagForwardOnly)

For Each objItem In colItems
Debug.Print "Caption: " & objItem.Caption
Debug.Print "CommandLine: " & objItem.CommandLine
Debug.Print "CreationClassName: " & objItem.CreationClassName
'Debug.Print "CreationDate: " &
WMIDateStringToDate(objItem.CreationDate)
Debug.Print "CSCreationClassName: " & objItem.CSCreationClassName
Debug.Print "CSName: " & objItem.CSName
Debug.Print "Description: " & objItem.Description
Debug.Print "ExecutablePath: " & objItem.ExecutablePath
Debug.Print "ExecutionState: " & objItem.ExecutionState
Debug.Print "Handle: " & objItem.Handle
Debug.Print "HandleCount: " & objItem.HandleCount
'Debug.Print "InstallDate: " &
WMIDateStringToDate(objItem.InstallDate)
Debug.Print "KernelModeTime: " & objItem.KernelModeTime
Debug.Print "MaximumWorkingSetSize: " & objItem.MaximumWorkingSetSize
Debug.Print "MinimumWorkingSetSize: " & objItem.MinimumWorkingSetSize
Debug.Print "Name: " & objItem.Name
Debug.Print "OSCreationClassName: " & objItem.OSCreationClassName
Debug.Print "OSName: " & objItem.OSName
Debug.Print "OtherOperationCount: " & objItem.OtherOperationCount
Debug.Print "OtherTransferCount: " & objItem.OtherTransferCount
Debug.Print "PageFaults: " & objItem.PageFaults
Debug.Print "PageFileUsage: " & objItem.PageFileUsage
Debug.Print "ParentProcessId: " & objItem.ParentProcessId
Debug.Print "PeakPageFileUsage: " & objItem.PeakPageFileUsage
Debug.Print "PeakVirtualSize: " & objItem.PeakVirtualSize
Debug.Print "PeakWorkingSetSize: " & objItem.PeakWorkingSetSize
Debug.Print "Priority: " & objItem.Priority
Debug.Print "PrivatePageCount: " & objItem.PrivatePageCount
Debug.Print "ProcessId: " & objItem.ProcessId
Debug.Print "QuotaNonPagedPoolUsage: " &
objItem.QuotaNonPagedPoolUsage
Debug.Print "QuotaPagedPoolUsage: " & objItem.QuotaPagedPoolUsage
Debug.Print "QuotaPeakNonPagedPoolUsage: " &
objItem.QuotaPeakNonPagedPoolUsage
Debug.Print "QuotaPeakPagedPoolUsage: " &
objItem.QuotaPeakPagedPoolUsage
Debug.Print "ReadOperationCount: " & objItem.ReadOperationCount
Debug.Print "ReadTransferCount: " & objItem.ReadTransferCount
Debug.Print "SessionId: " & objItem.SessionId
Debug.Print "Status: " & objItem.Status
'Debug.Print "TerminationDate: " &
WMIDateStringToDate(objItem.TerminationDate)
Debug.Print "ThreadCount: " & objItem.ThreadCount
Debug.Print "UserModeTime: " & objItem.UserModeTime
Debug.Print "VirtualSize: " & objItem.VirtualSize
Debug.Print "WindowsVersion: " & objItem.WindowsVersion
Debug.Print "WorkingSetSize: " & objItem.WorkingSetSize
Debug.Print "WriteOperationCount: " & objItem.WriteOperationCount
Debug.Print "WriteTransferCount: " & objItem.WriteTransferCount
Debug.Print
getOutlook = True
Next
Next

End Sub
 
Hi Jim,

that´s a lot of stuff just for looking whether an application is running
or not, don´t you think so?

:-)
 
Just showing how you can get more information with WMI. Put a wrapper on it
and use it as a code extension. It will also tell you if Outlook or any
other app is installed and the version and installation date etc. On stop
shopping once you build the query. You can add to the result by adjusting
the WSQL statement even store the statement in a file.

For thie simple answer the GetObject is the easiest way but since I was
looking for an easy methid and thought of WMI I thought I would share it.
The the "what is WMI?" question came up so here we are.

I believe that more knowledge about different ways to do things is very
helpfull to programming in general.
 
One important thisng I forgot to mention is it remotes. You can find the
information for every machine in the domain/forest assuming you have the
permission. It's a powerful and simple admiistration tool.
 
Hi Jim,

WMI is powerfull, that´s no question. But in this case, in Germany we
would call that "Shooting with cannons on sparrows" :-)
 
It's not really something a non-administrator would use in Office but it can
get you a solution when other things in Office won't work. It's especially
useful inside of Access for retrieving configuration data into a database.
This gives you a simple nd powerful reporting tool to use with the data. Of
course all of this can be done with scripting alone but I like the
Intellisense assistance. If I want a script I just paste code from VBA
into a file and, BANG, tested code to work from.
 
Back
Top