WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE and Outlook
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Use Existing Instance Or Create a New One If Necessary

 Keywords: ObjectCreate ObjectGet Existing Instance Create New Object 

Question:

I need to check if Outlook is running before running some OLE code and want to avoid errors if Outlook is not running. As Outlook seems to always have an ActiveExplorer when it is running, I have used the following:
; Setup connection to Outlook
myOlApp = ObjectCreate("Outlook.Application")
myExplorer = myOlApp.ActiveExplorer
myOlExp = myOlApp.ActiveExplorer
If myOlExp == 0 Then Exit ; Outlook not running
In some cases my script is a little slow to exit and some users may try to re-run the compiled script. Is there a faster way to detect if Outlook is running (across all versions)?

Answer:

This code will only create an instance of Outlook

; Check if Outlook is already running
ErrorMode(@OFF)
objOutlook = ObjectGet(,"Outlook.Application")
ErrorMode(@CANCEL)
error = LastError()
If objOutlook == 0 && error == 1129; Outlook is not already running
    Pause('Notice','Outlook not already running, creating an instance')
    objOutlook = ObjectCreate("Outlook.Application"); Create an instance of Outlook
EndIf
Pause('Notice','Add code to deal with Outlook')
objOutlook = 0
Exit


Article ID:   W18096
Filename:   Use Existing Instance Or Create a New One If Necessary.txt
File Created: 2011:01:27:11:56:52
Last Updated: 2011:01:27:11:56:52