none
COMException when using "Cancel = true;" at ItemEvents_10_BeforeAttachmentAddEventHandler RRS feed

  • Question

  • Hello everybody,

    I have a problem using "Cancel" at ItemEvents_10_BeforeAttachmentAddEventHandler.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Office = Microsoft.Office.Core;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Microsoft.Office.Interop.Outlook;
    
    namespace Test1
    {
        partial class FormRegion1
        {
            private Microsoft.Office.Interop.Outlook.MailItem mail = null;
            private Outlook.MailItem _mail = null;
    
            private void FormRegion1_FormRegionShowing(object sender, System.EventArgs e)
            {
                setMail(GetCurrentMailItem());
                set_Mail(GetCurrentMailItem());
                _mail.BeforeAttachmentAdd += new Microsoft.Office.Interop.Outlook.ItemEvents_10_BeforeAttachmentAddEventHandler(outlookMailItemEvent_BeforeAddAttachment);
    
                ContactItem ci = Globals.ThisAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olContactItem);
                ci.FirstName = "Test";
                ci.Save();
                if (mail != null)
                {
                    try
                    {
                        mail.Attachments.Add(ci);
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                        throw;
                    }  
                }
            }
    
            private void FormRegion1_FormRegionClosed(object sender, System.EventArgs e)
            {
            }
    
            private void outlookMailItemEvent_BeforeAddAttachment(Outlook.Attachment Attachment, ref bool Cancel)
            {
                Cancel = true;
            }
    
            public void setMail(Microsoft.Office.Interop.Outlook.MailItem mailItem)
            {
                mail = mailItem;
            }
    
            public void set_Mail(Outlook.MailItem mailItem)
            {
                _mail = mailItem;
            }
    
            private Microsoft.Office.Interop.Outlook.MailItem GetCurrentMailItem()
            {
                Microsoft.Office.Interop.Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
    
                if (inspector.CurrentItem != null)
                {
                    if (inspector.CurrentItem is Microsoft.Office.Interop.Outlook.MailItem)
                    {
                        Microsoft.Office.Interop.Outlook.MailItem newMail = inspector.CurrentItem;
    
                        if (!newMail.Sent)
                        {
                            return newMail;
                        }
                    }
                }
                return null;
            }
        }
    }
    If I comment out
    Cancel = true;

    everything works fine. But if I use this line an unhandled COMException occurs:
    "Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))"

    After leaving "outlookMailItemEvent_BeforeAddAttachment(Outlook.Attachment Attachment, ref bool Cancel)" the compiler jumps to "catch (System.Exception ex)" and the next step after  "throw;" was reached the exception occurs.

    I couldn't find any helpful information about this exception and I don't know why it occurs.
    Please help me to fix my code and understand the problem.

    Thanks in advance!

    Tuesday, October 22, 2013 3:27 PM

Answers

  • No, Attachments.Add either returns a valid Attachment object or raises an error. It does not return null.

    Try not simply use a try/catch block around the call to Attachments.Add?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by S.Koenigsberg Monday, November 4, 2013 11:14 AM
    Wednesday, October 30, 2013 4:37 PM

All replies

  • If you are adding the attachment programmatically by calling MailItem.Attachments.Add, would you not expect it to raise an exception since the Attachment object cannot be returned? If an exception is not raised, what would MailItem.Attachments.Add return? It cannot possibly be a valid attachment object since you explicitly disallowed the new attachment.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, October 22, 2013 5:50 PM
  • First of all thanks for your reply! And sorry for my delayed answer.

    I don't understand why and where my code should return an Attachment object. I thought "Cancel = true;" aborts the MailItem.Attachments.Add-methid, what means that this method must return nothing.

    I want my code to do the following:
    If the user klicks the icon to add a contact, an event should intercept and run some code. After this code is done, the contact should not be added. How can I do this without canceling the MailItem.Attachments.Add-method?

    Tuesday, October 29, 2013 3:11 PM
  • No, Attachments.Add either returns a valid Attachment object or raises an error. It does not return null.

    Try not simply use a try/catch block around the call to Attachments.Add?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by S.Koenigsberg Monday, November 4, 2013 11:14 AM
    Wednesday, October 30, 2013 4:37 PM