View Full Version : detect if Outlook is open
samohtwerdna
12-07-2005, 07:26 AM
Hello again,
I ran into a snag with my little schedule. I have several pop-up forms that utalize Outlook for sending reminders and sending emails based on cell entry. Everything works great if Outlook is open previously by the user - If not it runs in to a runtime error.
Is there a way to detect if Outlook is already open - and if it's not - it would really be great if I could open it for the user?
Any suggestions?
Bob Phillips
12-07-2005, 07:38 AM
Dim oOL As Object
    On Error Resume Next
    Set oOL = GetObject(, "Outlook.Application")
    On Error GoTo 0
    If oOL Is Nothing Then
        Set oOL = CreateObject("Outlook.Application")
        oOL.Visible = True
    End If
samohtwerdna
12-07-2005, 08:04 AM
Thanks xld,
Question: Why dimension oOL As an Object rather than as an Outlook.Application?
Also I get an error process not supported on the 
  oOL.Visible = True  
I have a reference set to Outlook 10.0
Bob Phillips
12-07-2005, 08:17 AM
I declared it as Object so as to save setting a reference in Tools>References. You could do that and then use a type of Outlook.Application if you wanted, but beware if your users are on different versions.
The .Visible line is an error (I adapted other code), so just bin it, it doesn't reallymatter.
When done, don't forget to clear the object variable down. You mightv also want to save a flag if you start Outlook, so that you can quit it at the end, leave it as you found it.
samohtwerdna
12-08-2005, 08:18 AM
Sub ShowCalendar()
    Dim olApp As Outlook.Application
    Dim olNs As NameSpace
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If Err.Number = 429 Then
        Set olApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0
    Set olNs = olApp.GetNamespace("MAPI")
    If olApp.ActiveExplorer Is Nothing Then
        olApp.Explorers.Add _
            (olNs.GetDefaultFolder(olFolderCalendar)).Activate
    Else
        Set olApp.ActiveExplorer.CurrentFolder = _
            olNs.GetDefaultFolder(olFolderCalendar)
        olApp.ActiveExplorer.Display
    End If
    Set olNs = Nothing
    Set olApp = Nothing
End Sub 
This is an example of one of the functions I have on my user form. If Outlook is open all works fine. If the user doesn't have Outlook Open I get a run time error saying that a dailog box is open please exit and try again. This is the line it stops on:
     If olApp.ActiveExplorer Is Nothing Then
        olApp.Explorers.Add _
            (olNs.GetDefaultFolder(olFolderCalendar)).Activate
    Else 
What is funny is that the code for this and my set Appointment - both work, but they open Outlook silently (meaning it's hidden) and then close it again when done. I would like to see the Outlook as well as manipulate it, and I have no idea what the solution to the "open dialog box" problem is...
Any thoughts??
Bob Phillips
12-08-2005, 09:25 AM
I don't get the problem so I cannot comment on that.
Outlook doesn't have a visible property, as we found, so we take a different approach. What we do is to open a folder, such as Inbox.
I have also added some code to wuit Outlook if it wasn't already opened, as I mentioned earlier.
Sub ShowCalendar()
    Dim olApp As Outlook.Application
    Dim olInbox As Outlook.MAPIFolder
    Dim olNs As NameSpace
    Dim fOLOpen As Boolean
     
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
     
    fOLOpen = Not olApp Is Nothing
    
    If Err.Number = 429 Then
        Set olApp = CreateObject("Outlook.Application")
    End If
     
    On Error GoTo 0
    
    Set olNs = olApp.GetNamespace("MAPI")
    
    'Make Outlook visible
    Set olInbox = olNs.GetDefaultFolder(olFolderInbox)
    olInbox.Display
    Set olInbox = Nothing
     
    If olApp.ActiveExplorer Is Nothing Then
        olApp.Explorers.Add _
        (olNs.GetDefaultFolder(olFolderCalendar)).Activate
    Else
        Set olApp.ActiveExplorer.CurrentFolder = _
        olNs.GetDefaultFolder(olFolderCalendar)
        olApp.ActiveExplorer.Display
    End If
    
    If Not fOLOpen Then olApp.Quit
     
    Set olNs = Nothing
    Set olApp = Nothing
     
End Sub
samohtwerdna
12-08-2005, 09:39 AM
Thanks xld!
I still get a run time error with the message:
"A dialog box is open. Close it and try again"
When I debug olInBox.Display is the line it stops on now. You said you don't get the problem - does that mean your's works fine without the error - or that you don't understand the error? 
I assume the first - i am using OfficeXP and the only other thing I can think of is the panda anti virus software may be causing the problem
Zack Barresse
12-08-2005, 09:45 AM
Can't, then, this ..
    If Err.Number = 429 Then 
        Set olApp = CreateObject("Outlook.Application") 
    End If
.. become this ..
    If Not fOLOpen Then Set olApp = CreateObject("Outlook.Application") 
 :dunno
Bob Phillips
12-08-2005, 10:02 AM
Can't, then, this ..
    If Err.Number = 429 Then 
        Set olApp = CreateObject("Outlook.Application") 
    End If
.. become this ..
    If Not fOLOpen Then Set olApp = CreateObject("Outlook.Application") 
 :dunno
It can, but he changed my original code that tests for nothing to test the error number, so I left it as he seemed to want it that way.
samohtwerdna
12-08-2005, 10:03 AM
Hi Zack,
I'm not sure the difference, but I still get the error :dunno
I am including a test worksheet that I use for testing the code - so you can see the problem first hand if you want.
Bob Phillips
12-08-2005, 10:11 AM
Hi Zack,
I'm not sure the difference, but I still get the error :dunno
I am including a test worksheet that I use for testing the code - so you can see the problem first hand if you want.
I tried your workbook, and the ShowCalendar routine works fine for me.
samohtwerdna
12-08-2005, 12:46 PM
Ok, so I did some messing around with Outlook and found the problem with the dialog box - I was missing an addin -which created a dialog box warning - anyway after fixing that problem I now get a new error
a run-time error #91
Object variable or with block variable not set
Stoping on:
 Set olNs = olApp.GetNamespace("MAPI") 
So I removed the On Error GoTo 0  just previous to this line. Now the code runs Outlook opens and then closes before I can see anything?
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.