How to get the mail item of the window instead of the one in the activeexplorer?

Add-in Express™ Support Service
That's what is more important than anything else

How to get the mail item of the window instead of the one in the activeexplorer?
 
Carlos Gonzalez




Posts: 25
Joined: 2009-12-22
My add-in displays at the bottom of the email message window.
I have been using this code to get the relevant mail item.

If IsNothing(CType(OutlookAppObj, Outlook._Application).ActiveExplorer) Then
Exit Sub
End If
Dim Selection As Outlook.Selection
Try
Selection = CType(OutlookAppObj, _
Outlook._Application).ActiveExplorer.Selection
Catch
Exit Sub
End Try

Dim item As Outlook.MailItem = Nothing

Try
If Not (Selection Is Nothing) Then
If Selection.Count > 0 Then
If Not (Selection.Item(1) Is Nothing) Then
If Selection.Item(1).Class = Outlook.OlObjectClass.olMail Then

item = CType(Selection.Item(1), Outlook.MailItem)


However, when a user has two email windows open and the one he is looking at is different fr om the active item in the explorer, then my add-in gives the incorrect information, because I am always using the ActiveExplorer selection.

I believe the way to fix this is for my add-in to look at the mail item of the active window where it's being displayed.
How do I get the MailItem object of the actual emai messae window wh ere my add-in instance is being displayed?

Thank you!
Posted 30 Jul, 2010 08:18:10 Top
Carlos Gonzalez




Posts: 25
Joined: 2009-12-22
I seem to have made some progress. I found an object calle InspectorObj which provides the current item.
The code below works.
However, I noticed that the event I am using ADXSelectionChange gets triggered once for every email window that is open. If I have 5 email windows and I change the current selection in the outlook explorer, I will process the code 6 times, once for each window, and once for the explorer. Is there a way to skip the code for those email windows that are already open, and only call it for the new windows?

Here's my current code:

' First try current window (inspector)
If Not IsNothing(Me.InspectorObj) Then
If Me.InspectorObj.CurrentItem.Class = Outlook.OlObjectClass.olMail Then
item = Me.InspectorObj.CurrentItem
End If
End If

' If we still don't have an item, revert to item in ActiveExplorer
If IsNothing(item) Then
If Not IsNothing(CType(OutlookAppObj, Outlook._Application).ActiveExplorer) Then
Dim Selection As Outlook.Selection
Try
Selection = CType(OutlookAppObj, Outlook._Application).ActiveExplorer.Selection
If Not (Selection Is Nothing) Then
If Selection.Count > 0 Then
If Not (Selection.Item(1) Is Nothing) Then
If Selection.Item(1).Class = Outlook.OlObjectClass.olMail Then
item = CType(Selection.Item(1), Outlook.MailItem)
End If
End If
End If
End If
Catch
Finally
If Not IsNothing(Selection) Then Marshal.ReleaseComObject(Selection)
End Try
End If
End If


If IsNothing(item) Then Exit Sub

' From this point forward we have a mail item
Posted 30 Jul, 2010 09:16:34 Top
Carlos Gonzalez




Posts: 25
Joined: 2009-12-22
I have a code that works now. I share it for feedback and for other users to use.
I use the ADXNewInspector to initialize my form on new email windows that the user opens.
I use the ADXSelectionChange to initialize my form on the Preview Pane.
My code which is in ProcessMailItem only gets called once for each item.




Private Sub ADXOlForm1_ADXNewInspector(ByVal sender As Object, ByVal args As AddinExpress.OL.ADXOlForm.ADXNewInspectorEventArgs) Handles Me.ADXNewInspector
Dim item As Outlook.MailItem = Nothing

Try

If Not IsNothing(Me.InspectorObj) Then
If Me.InspectorObj.CurrentItem.Class = Outlook.OlObjectClass.olMail Then
item = Me.InspectorObj.CurrentItem
End If
End If

If IsNothing(item) Then Exit Sub

' From this point forward we have a mail item
Me.ProcessMailItem(item)

Catch
Finally
If Not (item Is Nothing) Then
Marshal.ReleaseComObject(item)
End If
End Try


End Sub


Private Sub ADXOlForm1_ADXSelectionChange() Handles Me.ADXSelectionChange


Dim item As Outlook.MailItem = Nothing

Try

' First try current window (inspector)
' The code below did not work because the ActiveWindow does not give us the current window. It always returns the same even if several windows are open.
'If Not IsNothing(CType(OutlookAppObj, Outlook._Application).ActiveWindow) AndAlso TypeName(CType(OutlookAppObj, Outlook._Application).ActiveWindow) = "Inspector" Then
' Try
' If CType(OutlookAppObj, Outlook._Application).ActiveInspector.CurrentItem.class = Outlook.OlObjectClass.olMail Then
' item = CType(OutlookAppObj, Outlook._Application).ActiveInspector.CurrentItem
' End If
' Catch
' End Try
'End If

If Not IsNothing(Me.InspectorObj) Then
If Me.InspectorObj.CurrentItem.Class = Outlook.OlObjectClass.olMail Then
' Handled in the ADXNewInspector now. So we don't refresh all the windows every time the explorer selection changes.
Exit Sub
item = Me.InspectorObj.CurrentItem
End If
End If

' If we still don't have an item, revert to item in ActiveExplorer
If IsNothing(item) Then
If Not IsNothing(CType(OutlookAppObj, Outlook._Application).ActiveExplorer) Then
Dim Selection As Outlook.Selection
Try
Selection = CType(OutlookAppObj, Outlook._Application).ActiveExplorer.Selection
If Not (Selection Is Nothing) Then
If Selection.Count > 0 Then
If Not (Selection.Item(1) Is Nothing) Then
If Selection.Item(1).Class = Outlook.OlObjectClass.olMail Then
item = CType(Selection.Item(1), Outlook.MailItem)
End If
End If
End If
End If
Catch
Finally
If Not IsNothing(Selection) Then Marshal.ReleaseComObject(Selection)
End Try
End If
End If


If IsNothing(item) Then Exit Sub

' From this point forward we have a mail item
Me.ProcessMailItem(item)

Catch
Finally
If Not (item Is Nothing) Then
Marshal.ReleaseComObject(item)
End If
End Try


End Sub
Posted 30 Jul, 2010 10:58:51 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi Carlos,

It looks like you have got the solution. Thank you for sharing it for others!

Do you have any unanswered questions now?
.NET, Extended MAPI, Windows Phone & RT
Posted 02 Aug, 2010 06:39:49 Top