Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
817 views
in Technique[技术] by (71.8m points)

vba - Determining whether an existing Outlook instance is open

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...