deepImplement
deepImplement

Reputation: 13

How to get the active selected Outlook Mail Object in pywin32?

I want a script which takes the active selected Mail in Outlook and make an new response mail on this. But I can't find a function to get an active or selected object in pywin32. I just found these:

message.GetLast()

and some similar functions. it works but I need a reference to an active chosen mail.

I would choose a filter like "restrict" to find the Mail automatically but I don't know where to get the information for the filters automatically.

Upvotes: 0

Views: 289

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66316

Use Application.ActiveExplorer().Selection collection. Note that it can have zero items or more than one item:

import win32com.client # Make sure you have installed the pywin32 package

outlook = win32com.client.Dispatch("Outlook.Application")
selection = outlook.ActiveExplorer().Selection

if selection.Count > 0:
    message = selection.Item(1)
else:
    message = None

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

The message.GetLast() method gets the last item from a collection (Folder.Items). Instead, you need to get an Explorer instance by calling the ActiveExplorer method of the Outlook Application class which returns the topmost Explorer object on the desktop. Use this method to return the Explorer object that the user is most likely viewing. Then you may get the selected items in the Explorer window by getting the Selection object (see the corresponding property of the Explorer class), for example, the following VBA sample shows how to get selected items:

Sub GetSelectedItems() 
 Dim myOlExp As Outlook.Explorer 
 Dim myOlSel As Outlook.Selection 
 Dim MsgTxt As String 
 Dim x As Integer 
 MsgTxt = "You have selected items from: " 
 
 Set myOlExp = Application.ActiveExplorer 
 Set myOlSel = myOlExp.Selection 
 
 For x = 1 To myOlSel.Count 
   MsgTxt = MsgTxt & myOlSel.Item(x).SenderName & ";" 
 Next x 
 
 MsgBox MsgTxt 
 
End Sub

The Outlook object model is common for all programming languages, so you could easily find the required properties and methods that should be used.

Also you may find the Explorer.ActiveInlineResponse property which returns an item object representing the active inline response item in the explorer reading pane. This property returns null if no inline response is visible in the Reading Pane.

Upvotes: 0

Related Questions