Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining whether an existing Outlook instance is open

Tags:

excel

vba

outlook

After reading how to use automation to send a message, I'm unclear of whether it's possible to avoid opening a new instance of Outlook if I already have one opened. If so, I'm unsure of how to search for examples determining whether an existing Outlook instance is open.

-----Including the suggestion--------

I have the following snippet, but I found that I can't create the instance properly. I'm basically following this example. I'm either getting this screenshot, or the error of "User-defined type not defined." Any suggestions?

Sub Example()
    'Dim w As Outlook.Application

    Const ERR_APP_NOTRUNNING As Long = 429
    On Error Resume Next


' Handle Microsoft outlook
    Set w = GetObject(, "Outlook.Application")
    If Err = ERR_APP_NOTRUNNING Then
      'Set w = New Outlook.Application
      Set w = CreateObject("Outlook.Application")
    End If
End Sub
like image 694
stanigator Avatar asked Jun 26 '11 00:06

stanigator


3 Answers

I know this question has been answered, but I thought I'd add that applications like Outlook (and I believe PowerPoint as well) are single-instance applications -- there is no need to determine if Outlook is already open because you can only have one copy of Outlook running.

http://msdn.microsoft.com/en-us/library/aa164542(v=office.10).aspx

If you need to instantiate Outlook, simply use CreateObject to create the instance; if Outlook is already running, your object reference will point to the existing instance. If not, you will create the class. Binding (late or early) is irrelevant.

For example, let's say Outlook isn't running. We can use this code to create the instance:

Sub testOutlook()

Dim olApp As Object ' Outlook.Application

Set olApp = CreateObject("Outlook.Application")
  MsgBox (olApp2 Is Nothing)

End Sub

This will print "False" because we created the instance.

Let's say Outlook IS running. We can use this code to verify that using GetObject and CreateObject will refer to the existing instance:

Sub testOutlook()

Dim olApp As Object ' Outlook.Application
Dim olApp2 As Object ' Outlook.Application

Set olApp = GetObject(, "Outlook.Application")
  MsgBox (olApp Is Nothing)

Set olApp2 = CreateObject("Outlook.Application")
  MsgBox (olApp2 Is Nothing)
  MsgBox "Same object? " & (olApp Is olApp2)

End Sub

This will print "False" (existing instance), "False" (our alleged "new instance"), but the last message box is "True" because the new instance is actually the same object as the existing instance.

So what do we do if we don't know if Outlook is running or not? As demonstrated above, CreateObject either created a new instance (if one didn't exist, as in the first example) or hooked the existing instance if Outlook was already open (as in the second example).

like image 180
JimmyPena Avatar answered Oct 18 '22 09:10

JimmyPena


I see in your question that you commented out

'Dim w As Outlook.Application

presumably because this gives you the "User-defined type not defined" error.

This is likely because you have not set a reference to the Outlook library in your Excel-VBA project. This is done as follows: Tools > References > check "Microsoft Outlook xx.x Object Library". Then you can write this

Dim w As Outlook.Application
Set w = New Outlook.Application
' or, 
'Set w = CreateObject("Outlook.Application")

which, by the way, results in compile-time (or "early") binding. And gives you the Outlook object intellisense.

Alternatively, you can omit setting the reference and declare w as a generic object and let it bind at run-time

Dim w As Object
Set w = CreateObject("Outlook.Application")

but runtime (or "late") binding is less efficient.

Do whatever feels best -- I'm going to go ahead and venture that chances are, you won't notice the difference in efficency. I'm a recent convert to the early-binding thing, really just because of the intellisense.

EDIT So you've created a new Outlook application, but you can't see it. If you look in the Windows task manager, you'll see that the process is there, running -- but it's just not showing on the screen. Unfortunately, some brilliant engineer at Microsoft decided that Outlook shouldn't have a Visible property like Word or Excel do, so we have to use an awkward workaround. Open one of the special folders e.g. the Inbox like this:

Dim w As Outlook.Application
Dim wInbox As Outlook.MAPIFolder

Set w = New Outlook.Application
Set wInbox = w.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

wInbox.Display 'This makes Outlook visible
like image 43
Jean-François Corbett Avatar answered Oct 18 '22 10:10

Jean-François Corbett


    Set w = GetObject(, "Outlook.Application")

this should get running instance, if none is running catch error and do CreateObject

like image 1
vlscanner Avatar answered Oct 18 '22 11:10

vlscanner