0

I am working on a python for outlook. It connects through a win32com.client. I am trying to check whether the ActiveWindow in Outlook is a Mail in its own Window(Inspector) or the Preview inside the Mainwindow (Explorer). In VBA this is checked like here:

Dim curremail As Outlook.MailItem
Select Case TypeName(Application.ActiveWindow)

    Case "Explorer"
        Set curremail = Application.ActiveExplorer.Selection.Item(1)

    Case "Inspector"
        Set curremail = Application.ActiveInspector.CurrentItem

End Select

Unfortunately, the I don't know how to get the TypeName. The python type() returns <class 'win32com.client.CDispatch'> not the type of the ActiveWindow. Does anyone have an idea how to get the VBA TypeName, or if that is not possible how to determine whether the Mail is opened in the preview or its own window?

import win32com.client as client
outlook = client.Dispatch('Outlook.Application')

print(outlook.Inspectors.Count) #Can Determine the amount of open windows, but not if one is active
print(outlook.Inspectors.Item(1).Caption) #Caption of the first open window 

print(outlook.ActiveWindow())
print(type(outlook.ActiveWindow()))
Finn
  • 2,333
  • 1
  • 10
  • 21

2 Answers2

0

You can try to access ActiveWindow as Inspector, catch an exception (try/except), and access as Explorer if you end up in the exception handler.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

You can check the type of the ActiveWindow object at runtime:

check = isinstance(outlook.ActiveWindow, Inspector)

Or simpler:

check = type(outlook.ActiveWindow) is Inspector

See Determine the type of an object? for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45