Consulting

Results 1 to 15 of 15

Thread: How to tell if Outlook window is open in VBA?

  1. #1
    VBAX Regular
    Joined
    Feb 2022
    Posts
    9
    Location

    How to tell if Outlook window is open in VBA?

    Hello,

    Is there a way to tell if Outlook is open in the notification area (automate mode with wheel icon), or if there's an Outlook window already open?

    Thank you.

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    618
    Location
    Option Explicit
    
    Sub TestOutlookIsOpen()
        Dim oOutlook As Object
    
    
        On Error Resume Next
        Set oOutlook = GetObject(, "Outlook.Application")
        On Error GoTo 0
    
    
        If oOutlook Is Nothing Then
            MsgBox "Outlook is not open, open Outlook and try again"
        Else
            MsgBox "Outlook is open."
        End If
    End Sub

  3. #3
    VBAX Regular
    Joined
    Feb 2022
    Posts
    9
    Location
    Hi Logit. Thanks for the reply, but I do have similar code already. My problem is that this method only tells me if the application is running, but not if the Outlook window is open.
    I have a macro that opens an instance of Outlook (hidden in the system tray, in automation mode), runs some code and at one point it calls an inspector, which requires a window to be open, otherwise it fails.
    So, I open one with .Display, but if there's a window already visible, it opens a second one, and I'm trying to prevent that by detecting if Outlook is on the system tray or if there's a window active.
    I hope I explained myself clearly.
    Is there a built-in method for that in VBA?

  4. #4
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    618
    Location
    What I am finding on the internet is not working as stated in their posting.

    What about checking to see if Outlook is running ... if not, start it. Obviously, Outlook would be 'open' at that point.

    If it is already running, you could stop that process, then re-open it ?

  5. #5
    You could check the activewindow.class e.g.
    Dim lClass As Long
        lClass = Outlook.Application.ActiveWindow.Class
        If lClass = 34 Then 'The activewindow is an Explorer object
    The full list of results is at https://docs.microsoft.com/en-us/off....OlObjectClass
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    618
    Location
    gmayor :

    I looked at a similar macro (as yours) but if you are running the macro from a separate workbook, then the "activewindow" would be that excel workbook and not Outlook.

    How would you get vba to recogize Outlook as the active window ?

  7. #7
    If you are working from Excel and Outlook is open, then
    Dim olApp As Object
    Dim lClass As Long
        Set olApp = GetObject(, "Outlook.Application")
        lClass = olApp.ActiveWindow.Class
        If lClass = 34 Then  'The Outlook active window is an Explorer object
    If Outlook is not open then you would have to open it and select the required item in Outlook, which would make this code superfluous. In fact it is undoubtedly better to select what you wish to process in either instance.
    In that case look to ensure that Outlook is correctly opened from Excel - see http://www.rondebruin.nl/win/s1/outlook/openclose.htm
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    VBAX Regular
    Joined
    Feb 2022
    Posts
    9
    Location
    Hi gmayor,

    That ObjectClass method works very well. Do you know if I can do something similar with Word? Was not able to find an equivalent property.

  9. #9
    Excel or Word, the coding to process Outlook would be similar.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  10. #10
    VBAX Regular
    Joined
    Feb 2022
    Posts
    9
    Location
    I meant if I want to test an open Word application window, instead of Outlook. I looked at the Word Object references but seem to be very different.

  11. #11
    OK. Simply set a variable to open the named document. Word, unlike Excel, simply switches to that document if it is already open.
    Dim oDoc As Document
    Set oDoc = Documents.Open(FileName:="C:\Path\filename.docx", AddtoRecentFiles:=False)
    'do something with oDoc
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  12. #12
    VBAX Regular
    Joined
    Feb 2022
    Posts
    9
    Location
    I see. Thanks for your help gmayor and Logit!

  13. #13
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    618
    Location
    You are welcome.

  14. #14
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    618
    Location
    Aljhone

    What does your provided link have to do with the OP question ?

  15. #15
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,232
    Location
    @logit. aljhone has hit the showers, having been benched from the game.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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