Results 1 to 11 of 11
  1. #1
    Gold Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    2,639
    Thanks
    115
    Thanked 651 Times in 593 Posts

    Test for instance of Outlook running

    Much of the code I write for work and some recent lounge threads involves opening/sending email, task reminders, appointments, etc. from within Excel. All are dependent upon an instance of Outlook running prior to my code being implemented or the parts of the code that are Outlook related fail. I have not found a reliable way to test if Outlook is running to defer the code if it is not. Standard error checking doesn't seem to cut the mustard.

    If someone has figured this one out, it would be greatly appreciated. Keep in mind that I am running the code from within Excel not Outlook.

    Thanks in advance
    Maud

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Maud,

    See if this MS Article will help.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Gold Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    2,639
    Thanks
    115
    Thanked 651 Times in 593 Posts
    RG,

    You da man!!! Never looked into the API for the answer.

    There wasn't the class name for Outlook in the list but a quick search found "rctrl_renwnd32". Modifying the code to test for the handle, if the returned value =0 then Oultook is not running. If 985408 is returned then Outlook is running. A working test snippet is:

    Code:
    Option Explicit
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    
    Public Sub MyProcedure()
    Dim OutlookInstance As Long
    OutlookInstance = FindWindow("rctrl_renwnd32", 0&)
    If OutlookInstance <> 0 Then
        MsgBox "Outlook is running"
        '--------code----------
    Else:
        MsgBox "Outlook is not running"
        Exit Sub
    End If
    End Sub
    This solves a big time problem. If they didn't take away the thank you button, I would have pressed it 20 times.

    Maud
    Last edited by Maudibe; 2014-06-28 at 17:46.

  4. #4
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    If you could run the built-in TASKLIST.EXE and looked for an ImageName of OUTLOOK.EXE, would that be any good?

    As in
    tasklist /fi "imagename eq outlook.exe"
    Last edited by BATcher; 2014-06-28 at 16:40.
    BATcher

    Time prevents everything happening all at once...

  5. #5
    Gold Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    2,639
    Thanks
    115
    Thanked 651 Times in 593 Posts
    What a great thought! Definitely thinking outside the box. But how to run a command line utility with a switch from VBA and return a value? An interesting project for a rainy day....but it is sunny out now and getting ready to 'que a thick juicy steak!

    Thanks,
    Maud

  6. #6
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts
    How about something like this?
    Code:
    Dim OutApp As Outlook.Application
    On Error Resume Next
    Set OutApp = GetObject(, "Outlook.Application")
    If OutApp Is Nothing Then
    Set OutApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo ErrHandler
    Steve

  7. #7
    Gold Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    2,639
    Thanks
    115
    Thanked 651 Times in 593 Posts
    sdckapr,

    Looks like one of the snippets that I had tried previously with multiple variations but not sure if it is exactly like you posted. May be worth revisiting it again. Thanks.

    The above code using the windows API worked flawlessly and by following RG's link, there is a list of frequently used apps and their class names that the code can be applied to. That makes it very versatile.

    Maud

  8. #8
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    Quote Originally Posted by Maudibe View Post
    What a great thought! Definitely thinking outside the box.
    Not my box! The Command Line Interface still lives.

    Quote Originally Posted by Maudibe View Post
    But how to run a command line utility with a switch from VBA and return a value?
    As frequently happens in academic tomes, "this is left as an exercise for the reader..."
    BATcher

    Time prevents everything happening all at once...

  9. #9
    5 Star Lounger
    Join Date
    Jan 2004
    Location
    Praha
    Posts
    990
    Thanks
    56
    Thanked 105 Times in 90 Posts
    What an interesting thread !

    I found some good advice here - worked for me: http://www.rondebruin.nl/win/s1/outlook/openclose.htm

  10. #10
    Gold Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    2,639
    Thanks
    115
    Thanked 651 Times in 593 Posts
    Martin,

    I obviously need to sharpen my google skills. And to boot, I reference Ron's site frequently as the bible of Outlook VBA. That worked great and was easier to employ then my piecemeal code. Thanks

    sdckapr,

    I retested you code and it worked equally as well after I referenced the Microsoft Outlook 14.0 Object Library. This was the issue that I was experiencing before. I was looking for an "out of the box" solution that I never reference additional libraries to get them to work. Thanks also

    Maud

  11. #11
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts
    I think if you dim as object, the reference may not be required...

    I am sure you are aware, but for others following the thread who may not have noticed, the deBruin method linked and what I posted use the principle. The cited code informs that there is no open outlook running. The code I posted, goes a step further, presuming that you want it to be running, so it then opens outlook.

    Steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •