ContextMenuAttachment get list of selected attachments on Button click

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

ContextMenuAttachment get list of selected attachments on Button click
 
Dishant Arora




Posts: 11
Joined: 2010-06-07
Hi,

We have outlook addins which saves email attachment.

I can get list of selected attachment file in Outlook 2007 and Outlook 2010.
Using following code

Private Sub adxOutlookEvents_AttachmentContextMenuDisplay(sender As Object, commandBar As Object, target As Object) Handles adxOutlookEvents.AttachmentContextMenuDisplay
        Dim attachmentsSelectionObj As Object = Nothing
        Dim attachedFile As Object = Nothing
        Try
            attachmentsSelectionObj = target
            arrListFileAttachment = Nothing
            arrListFileAttachment = New ArrayList
            Dim count As Integer = attachmentsSelectionObj.Count
            If count = 0 Or count > 1 Then
                AdxCommandBarButtonSaveAttachment.Visible = False
            End If
            For i = 1 To attachmentsSelectionObj.Count
                attachedFile = attachmentsSelectionObj(i)
                Dim fileName As String = attachedFile.FileName
                Dim fileExt As String = UCase(Path.GetExtension(fileName))
                If fileExt = ".DOC" Or fileExt = ".DOCX" Or fileExt = ".PDF" Or fileExt = ".XLS" Or fileExt = ".XLSX" Then
                    arrListFileAttachment.Add(attachedFile)
                    AdxCommandBarButtonSaveAttachment.Visible = True
                End If
            Next
        Catch ex As Exception
            objAcumenGlobalClass.CREATEERRORFILE("[Error: ] " + "- [ " + System.Reflection.MethodInfo.GetCurrentMethod.Name + " ] " + ex.Message + " , " + ex.StackTrace, objAcumenUserVariables)
            objAcumenGlobalClass.DisplayErrorMsg(ex.Message, ex, System.Reflection.MethodInfo.GetCurrentMethod.Name, "", objAcumenUserVariables)
        Finally
            attachmentsSelectionObj = Nothing
            attachedFile = Nothing
        End Try
    End Sub


But in Outlook 2013 Event AttachmentContextMenuDisplay does not fire.
How can I get list of selected Attachment ? Following Code gives me all attachments but we want selected attachment list only

Private Sub AdxRibbonButtonSaveAttachment_OnClick(sender As Object, control As AddinExpress.MSO.IRibbonControl, pressed As Boolean) Handles AdxRibbonButtonSaveAttachment.OnClick
        Try
            Dim attachmentInfo As StringBuilder = New StringBuilder()
            Dim mailAttachments As Outlook.Attachments = control.Context.Parent.Attachments
            If Not IsNothing(mailAttachments) Then
                For i As Integer = 1 To mailAttachments.Count
                    Dim currentAttachment As Outlook.Attachment = mailAttachments.Item(i)
                    If Not IsNothing(currentAttachment) Then
                        attachmentInfo.AppendFormat("#{0}", i)
                        attachmentInfo.AppendLine()
                        attachmentInfo.AppendFormat("File Name: {0}", currentAttachment.FileName)
                        attachmentInfo.AppendLine()
                        attachmentInfo.AppendFormat("Diplay Name: {0}", currentAttachment.DisplayName)
                        attachmentInfo.AppendLine()
                        attachmentInfo.AppendFormat("Type: {0}", currentAttachment.Type)
                        attachmentInfo.AppendLine()
                        attachmentInfo.AppendLine()
                        Marshal.ReleaseComObject(currentAttachment)
                    End If
                Next
                If attachmentInfo.Length > 0 Then
                    System.Windows.Forms.MessageBox.Show( _
                        attachmentInfo.ToString(), "E-mail attachments")
                    Marshal.ReleaseComObject(mailAttachments)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub


Can you please let me know how we can get list of selected attachment ?

Thanks,
Dishant
Dishant
Posted 30 Nov, 2015 01:07:47 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Dishant,

I assume you need to get the list of selected attachments in the Click event of a button shown in the ContextMenuAttachments context menu. If so, you need to get the context in which the control is displayed (check section Determining a Ribbon Control's Context, see the PDF file in the folder {Add-in Express}\Docs on your development PC). If the context object is AttachmentSelection, you get the selected attachments. If the context object is Explorer or Inspector, call Explorer.AttachmentSelection or Inspector.AttachmentSelection first.


Andrei Smolin
Add-in Express Team Leader
Posted 30 Nov, 2015 03:41:20 Top
Dishant Arora




Posts: 11
Joined: 2010-06-07
Hi Andrei,

Yes that's correct.
Can you please send me sample code.

Documentation suggest you can use PropertyChanging event but when we click or open context menu its not firing property change event.

Thanks,
Dishant
Dishant
Posted 01 Dec, 2015 00:38:36 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Dishant,

I've created this button in this Ribbon context menu:


            // 
            // adxRibbonContextMenu1
            // 
            this.adxRibbonContextMenu1.ContextMenuNames.AddRange(new string[] {
            "Outlook.Explorer.ContextMenuAttachments",
            "Outlook.Mail.Read.ContextMenuAttachments"});
            this.adxRibbonContextMenu1.Controls.Add(this.adxRibbonButton1);
            this.adxRibbonContextMenu1.Ribbons = ((AddinExpress.MSO.ADXRibbons)(((AddinExpress.MSO.ADXRibbons.msrOutlookMailRead | AddinExpress.MSO.ADXRibbons.msrOutlookMailCompose) 
            | AddinExpress.MSO.ADXRibbons.msrOutlookExplorer)));
            // 
            // adxRibbonButton1
            // 
            this.adxRibbonButton1.Caption = "adxRibbonButton1";
            this.adxRibbonButton1.Id = "adxRibbonButton_4a060117645443fabc3e9353127c92cd";
            this.adxRibbonButton1.ImageTransparentColor = System.Drawing.Color.Transparent;
            this.adxRibbonButton1.Ribbons = ((AddinExpress.MSO.ADXRibbons)(((AddinExpress.MSO.ADXRibbons.msrOutlookMailRead | AddinExpress.MSO.ADXRibbons.msrOutlookMailCompose) 
            | AddinExpress.MSO.ADXRibbons.msrOutlookExplorer)));
            this.adxRibbonButton1.OnClick += new AddinExpress.MSO.ADXRibbonOnAction_EventHandler(this.adxRibbonButton1_OnClick);


Outlook 2016 introduced the Ribbon context menu for attachments in the compose inspector. To handle this I add this code:

private void AddinModule_OnRibbonBeforeCreate(object sender, string ribbonId)
{
    if (this.HostMajorVersion > 15)
    {
        this.adxRibbonContextMenu1.ContextMenuNames.Add("Outlook.Mail.Compose.ContextMenuAttachments");
        this.adxRibbonContextMenu1.Ribbons |= ADXRibbons.msrOutlookMailCompose;
        this.adxRibbonButton1.Ribbons |= ADXRibbons.msrOutlookMailCompose;
        // System.Diagnostics.Debug.WriteLine("!!! " + this.adxRibbonButton1.Ribbons.ToString()); 
    }
}


Now, you can start Outlook and check that this button is shown when you right-click an attachment(s) in these contexts:
- in the Preview pane (Outlook 2010+)
- in the read inspector (Outlook 2010+)
- in the compose inspector (Outlook 2016 only)

In the Click event of that button I get selected attachments in this way:

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)
{
    int result = 0;
    object context = control.Context;

    if (context is Outlook._Explorer)
    {
        Outlook._Explorer window = context as Outlook._Explorer;
        Outlook._AttachmentSelection attSelection = window.AttachmentSelection;
        result = attSelection.Count;
        Marshal.ReleaseComObject(attSelection);
    }
    else if (context is Outlook._Inspector)
    {
        Outlook._Inspector window = context as Outlook._Inspector;
        Outlook._AttachmentSelection attSelection = window.AttachmentSelection;
        result = attSelection.Count;
        Marshal.ReleaseComObject(attSelection);
    }
    else if (context is Outlook._AttachmentSelection)
    {
        Outlook._AttachmentSelection attSelection = context as Outlook._AttachmentSelection;
        result = attSelection.Count;
        Marshal.ReleaseComObject(attSelection);
    }
    else
    {
        result = -1;
    }

    if (context == null)
    {
        result = int.MinValue;
    }
    else
    {
        Marshal.ReleaseComObject(context);
    }
    MessageBox.Show("Selected attachment count = " + result.ToString());
}



Andrei Smolin
Add-in Express Team Leader
Posted 01 Dec, 2015 03:13:40 Top