Which Email is selected

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

Which Email is selected
 
SSL




Posts: 178
Joined: 2014-01-05
Hi Andrei,

Further to an older post, you mentioned:

When you select an email, the SelectionChange event occurs on the Explorer object representing the Outlook Explorer window in which you select the email. In add-in Express, this email is mapped to the ExplorerSelectionChange event of the Outlook Events component; you need to put it onto the add-in module.

Starting from Outlook 2010, you can manipulate the selection programmatically, see Explorer.AddToSelection(), Explorer.RemoveFromSelection() and Explorer.ClearSelection() methods.

See also http://msdn.microsoft.com/en-us/library/office/ff868732%28v=office.15%29.aspx.


I dont have "Explorer.AddToSelection(), Explorer.RemoveFromSelection() and Explorer.ClearSelection() methods. " availble to me, my addin is outlook 2007 onwards.

Can this be achieved another way?

Regards,

Tom
Posted 27 Feb, 2015 07:29:00 Top
Andrei Smolin


Add-in Express team


Posts: 18806
Joined: 2006-05-11
Hello Tom,

In Outlook 2007 there's no way to use these methods. If your add-in gets loaded in Outlook 2010 and above, you can can these methods using late binding, see System.type.InvokeMeber on MSDN.


Andrei Smolin
Add-in Express Team Leader
Posted 27 Feb, 2015 07:48:02 Top
SSL




Posts: 178
Joined: 2014-01-05
Hi Andrei,

Thanks for that, I suppose what I am trying to achieve is which reply, replyall or forward button has been selected ie from Explorer or Inspector.

Is it possible to know?

Regards,

Tom
Posted 27 Feb, 2015 08:22:18 Top
Andrei Smolin


Add-in Express team


Posts: 18806
Joined: 2006-05-11
You can intercept the corresponding Ribbon commands; the Explorer/Inspector where the button is clicked is available using control.Context. See also sections Intercepting Built-in Ribbon Controls and Determining a Ribbon Control's Context in the manual.


Andrei Smolin
Add-in Express Team Leader
Posted 27 Feb, 2015 08:31:57 Top
SSL




Posts: 178
Joined: 2014-01-05
Hi Andrei,

I have decided to try (Depending on which is active Explorer or Inspector, I can access the selected email that way.)

Would this be a correct method:

#Region "Get Selected Mail Item"
    Public Function GetSelectedMailItem() As Outlook.MailItem

        Dim email As Outlook.MailItem = Nothing
        Dim ActiveExplorer As Outlook.Explorer = Nothing
        Dim currentItem As Object = Nothing

        Dim ActiveInspector As Outlook._Inspector = Nothing
        Dim olItem As Object = Nothing


        ActiveInspector = Solution_Outlook.AddinModule.Instance.OutlookHost().ActiveInspector

        If Not ActiveInspector Is Nothing Then

            olItem = ActiveInspector.CurrentItem

            Try
                If TypeOf (olItem) Is Outlook.MailItem Then

                    email = CType(ActiveInspector.CurrentItem, Outlook.MailItem)
                    Return email
                End If
            Finally
                Marshal.ReleaseComObject(email)
            End Try

        Else

            Dim currentExplorer As Outlook._Explorer = DirectCast(AddinModule.Instance.OutlookApp.ActiveExplorer, Outlook.Explorer)

            If currentExplorer IsNot Nothing Then
                Dim selection As Outlook.Selection = currentExplorer.Selection
                Try
                    If selection.Count > 0 Then
                        Dim selectedItem As Outlook.MailItem = TryCast(selection.Item(1), Outlook.MailItem)
                        If selectedItem IsNot Nothing Then
                            Return selectedItem
                        End If
                    End If
                Finally
                    Marshal.ReleaseComObject(selection)
                End Try
            End If
        End If

        Return Nothing
    End Function
#End Region


I dont think its quite right.

Regrds,

Tom
Posted 27 Feb, 2015 10:24:17 Top
Andrei Smolin


Add-in Express team


Posts: 18806
Joined: 2006-05-11
Tom,

1. Inspector-related

a) Marshal.ReleaseComObject(email) release the MailItem this method returns. This means the caller won;t be able to use it.

b)You need to release oItem if it contains any other Outlook type: TaskItem, ContactItem, etc.

2. Explorer-related

a)
Dim currentExplorer As Outlook._Explorer = DirectCast(AddinModule.Instance.OutlookApp.ActiveExplorer, Outlook.Explorer)

This will fail if there's no active explorer. Just remove the DirectCast to avoid this issue.

b)You need to release selection.Item(1) if it contains any other Outlook type: TaskItem, ContactItem, etc.

c)
Dim selection As Outlook.Selection = currentExplorer.Selection

This will fail when are are in certain folders, e.g. RSS Feeds (depends on the Outlook version used). You need to wrap currentExplorer.Selection in a try/catch block and check if the result is not Nothing.


Andrei Smolin
Add-in Express Team Leader
Posted 27 Feb, 2015 10:37:24 Top