Announcement

Collapse
No announcement yet.

Is Outlook Open

Collapse
This topic is closed.
X
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is Outlook Open

    How to check from Excel VBA if Outlook is open and running?
    This was an old question with post http://www.ozgrid.com/forum/showthread.php?t=44790
    It was not closed with any answer or solution. I had the same problem so here is a little trick to solve it. Code works, but I just don't like how it looks like.

    If you have a better solution please post it here. There is not one good examle on the web.
    I'm sure there must be something like:
    IF NOT Application(Outlook).Open Then Open it .....

    Code:
    '*****************************************************************
    ' ?? IS OUTLOOK RUNNING ??
    ' 7/24/07 by Dusan J.
    '
    ' Problem:  To download attachment we need Outlook to be open.
    '   I didn't know how to check for open application so
    '   I used a little trick to solve this.
    ' Solution: Try to make Outlook Application active window.
    '   If Oulook is not open, that generates Run time error 91.
    '   On Error run a code to open Outlook.
    '******************************************************************
    Sub CheckIfOutlookIsRunning()
        On Error GoTo OutlookIsNotRunning
        AppActivate ("Outlook")
        Exit Sub
         
    OutlookIsNotRunning:
    '    Dim OutlookErr, OutlookBox
    '    OutlookErr = "This program requires Outlook to be running on your computer." & vbCrLf & vbCrLf
    '    OutlookErr = OutlookErr & "Please open Outlook and try again."
    '    OutlookBox = MsgBox(OutlookErr, vbCritical, "Outlook Is Not Open")
    ' Instead of Error message just open Outlook
        Call Open_OutLook
        End
    End Sub
    '
    Sub Open_OutLook()
       Dim oOutlook As Object
       Dim oNameSpace As Object
       Dim oInbox As Object
    
       Set oOutlook = CreateObject("Outlook.Application")
       Set oNameSpace = oOutlook.GetNamespace("MAPI")
       Set oInbox = oNameSpace.Folders(1)
       'Set oInbox = oInbox.Folders("Inbox")
       oInbox.Display
       'oOutlook.Quit 'Close All Outlook copies
    End Sub
    '---------------------------------------------------------

    Auto Merged Post;

    Doe's anyone has a real code/solution for something like this?
    Last edited by dusanj; July 27th, 2007, 06:00. Reason: Auto Merged Doublepost

  • #2
    Re: Is Outlook Open

    Originally posted by dusanj
    ...Code works, but I just don't like how it looks like.

    ...I'm sure there must be something like:
    IF NOT Application(Outlook).Open Then Open it .....
    This is essentially an If Not open then open it.. Anyway it _looks_ better <G>


    Code:
    Sub CIOIR()
        
    Dim oOutlook As Object
    Dim oNameSpace As Object
    Dim oInbox As Object
    
    Set oOutlook = CreateObject("Oulook.Application")
    Set oNameSpace = oOutlook.GetNamespace("MAPI")
    Set oInbox = oNameSpace.Folders(1)
        
       
        'Handle errors
         Err.Clear 'JIC
         On Error Resume Next
        
        'attempt
        AppActivate ("Outlook")
        
        If Err <> 0 Then oInbox.Display
        
        'reset
        Err.Clear
        On Error GoTo 0
        
        'cleanup
        Set oInbox = Nothing
        Set oOutlook = Nothing
        Set oNameSpace = Nothing
        
    End Sub
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http://www.ExcelVBA.joellerabu.com

    Comment


    • #3
      Re: Is Outlook Open

      OK, cleaner code but ...
      - You have typo; missing "t" in ("Oulook.Application")
      - Also if Outlook is Open your code opens another copy of outlook. It would be nice not to do that.

      Comment


      • #4
        Re: Is Outlook Open

        Please at least take the time to use the correct forum.

        Comment


        • #5
          Re: Is Outlook Open

          After thinking about this more I fond the least amount of code I can use. I don't have to check if outllok is open; just close it first (if it is closed this doesn't genereta any error), and than open it. Here is all code you need. I don't know what kind of Error I can expect.

          Code:
          Sub Open_OutLook()
             Dim oOutlook As Object
             Dim oNameSpace As Object
             Dim oInbox As Object
          
             Set oOutlook = CreateObject("Outlook.Application")
             Set oNameSpace = oOutlook.GetNamespace("MAPI")
             Set oInbox = oNameSpace.Folders(1)
          
             oOutlook.Quit                                'Close All Outlook copies if open
             oInbox.Display                               'Open/show Outlook
          
          End Sub

          Comment

          Working...
          X