How to get a selected attachment and the associated MailItem

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

How to get a selected attachment and the associated MailItem
 
Daniel Tietze




Posts: 37
Joined: 2008-09-10
Hi,

I use following Event from AttachmentContextMenu Button. It get fired. But how do i get the attachment and the associated mailItem?


 Friend Sub save_Attachment_Ribbon(ByVal sender As Object, ByVal control As AddinExpress.MSO.IRibbonControl, ByVal pressed As Boolean) Handles AdxRibbonButton1SaveAttachment.OnClick


'Something must happen here


     
    End Sub



Thanks,
Daniel
Posted 19 Oct, 2012 05:50:56 Top
Andrei Smolin


Add-in Express team


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

    Private Sub AdxRibbonButton1_OnClick(ByVal sender As System.Object, ByVal control As AddinExpress.MSO.IRibbonControl, ByVal pressed As System.Boolean) Handles AdxRibbonButton1.OnClick
        Dim context As Object = control.Context
        System.Diagnostics.Debug.Print("### Context is " + Microsoft.VisualBasic.Information.TypeName(context))
        If (TypeOf context Is Outlook.AttachmentSelection) Then
            Dim selection As Outlook.AttachmentSelection = CType(context, Outlook.AttachmentSelection)
            System.Diagnostics.Debug.Print("### AttachmentSelection.Count = " + selection.Count.ToString())
            For i As Integer = 1 To selection.Count
                Dim attachment As Outlook.Attachment = selection.Item(i)
                System.Diagnostics.Debug.Print("### Attachment #" + i.ToString() + ". DisplayName = " + attachment.DisplayName)
                Marshal.ReleaseComObject(attachment)
            Next
        End If
        Marshal.ReleaseComObject(context)
    End Sub


Hope this helps.


Andrei Smolin
Add-in Express Team Leader
Posted 19 Oct, 2012 06:52:52 Top
Daniel Tietze




Posts: 37
Joined: 2008-09-10
Hello Andrei,

This worked pretty well.

I?m trying to do the same thing with Outlook 2007. I read that I have to use a context menu "Attachment Context Menu". My added button is shown there, but the call of the sub by the fired click event is different.

How do I get the objects as above in this case?

    Friend Sub save_Attachment_Button(ByVal sender As Object) Handles AdxCommandBarButton6SaveAttachment.Click






    End Sub


Thanks,
Daniel
Posted 22 Oct, 2012 09:53:07 Top
Andrei Smolin


Add-in Express team


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

Below is a code sample demonstarting the idea how to deal with selected attachments in Outlook 2007. I'm sorry, I have the C# version only. You can use any online convertor from CSharp to VB:

        private void adxOutlookEvents_AttachmentContextMenuDisplay(object sender, object commandBar, object target)
        {
            // get selected attachments 
            Outlook.AttachmentSelection attachmentsSelectionObj = target as Outlook.AttachmentSelection;
            //attachmentsSelectionObj will be released after the method ends, so you need to get attachments here
            atts = new Microsoft.Office.Interop.Outlook.Attachment[attachmentsSelectionObj.Count];
            for (int i = 1; i <= attachmentsSelectionObj.Count; i++)
            {
                atts[i - 1] = attachmentsSelectionObj[i];
            }

            // get controls shown in the context menu
            Office.CommandBar cmBar = commandBar as Office.CommandBar;
            Office.CommandBarControls controls = cmBar.Controls;
            for (int i =1; i<=controls.Count; i++)
            {
                Office.CommandBarControl control = controls[i];
                controlNames += control.Caption + " " + control.Id + "; ";
                if (control.Caption =="&Save As...") 
                {
                    btnSaveAs = control as Office.CommandBarButton;
                    btnSaveAs.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(btnSaveAs_Click);
                    //btnSaveAs is relesed in adxOutlookEvents_ContextMenuClose
                }
                else 
                    Marshal.ReleaseComObject(control);
            }
            Marshal.ReleaseComObject(controls);

            //attachments are released in adxOutlookEvents_ContextMenuClose
        }

        void btnSaveAs_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            if (Ctrl.Caption == "&Save As...") // this check is required!
            {
                CancelDefault = true;
                System.Windows.Forms.MessageBox.Show("!");
            }
        }

        //test button
        private void adxCommandBarButton1_Click(object sender)
        {
            string attachmentNames = "";
            if (atts != null)
            {
                for (int i = 0; i < atts.Length; i++)
                {
                    Outlook.Attachment att = atts[i];
                    attachmentNames += att.FileName;
                }
            }
            System.Windows.Forms.MessageBox.Show(attachmentNames);
            System.Windows.Forms.MessageBox.Show(controlNames);
        }

        private void adxOutlookEvents_ContextMenuClose(object sender, AddinExpress.MSO.ADXOlContextMenu contextMenu)
        {
            if (btnSaveAs != null)
            {
                btnSaveAs.Click -= btnSaveAs_Click;
                Marshal.ReleaseComObject(btnSaveAs);
                btnSaveAs = null;
            }
            if (atts != null)
            {
                for (int i = 0; i < atts.Length; i++)
                {
                    Marshal.ReleaseComObject(atts[i]);
                }
                atts = null;
            }
            controlNames = "";
        }


Hope this helps.


Andrei Smolin
Add-in Express Team Leader
Posted 23 Oct, 2012 04:43:16 Top