PDA

View Full Version : How to tell if Outlook window is open in VBA?



dma
02-28-2022, 07:00 AM
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.

Logit
02-28-2022, 11:25 AM
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

dma
02-28-2022, 02:28 PM
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?

Logit
02-28-2022, 03:27 PM
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 ?

gmayor
02-28-2022, 10:04 PM
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/office/vba/api/Outlook.OlObjectClass

Logit
03-01-2022, 09:52 AM
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 ?

gmayor
03-01-2022, 10:05 PM
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

dma
03-02-2022, 05:42 AM
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.

gmayor
03-02-2022, 06:08 AM
Excel or Word, the coding to process Outlook would be similar.

dma
03-02-2022, 07:56 AM
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.

gmayor
03-02-2022, 10:28 PM
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

dma
03-03-2022, 06:05 AM
I see. Thanks for your help gmayor and Logit!

Logit
03-03-2022, 10:29 AM
You are welcome.

Logit
05-21-2022, 07:43 AM
Aljhone (http://www.vbaexpress.com/forum/member.php?84629-Aljhone)

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

Aussiebear
05-21-2022, 10:11 AM
@logit. aljhone has hit the showers, having been benched from the game.