none
Outlook Add-in problem: First email in Outbox gets stuck

    Question

  • I'm having a problem with an add-in we make. When "Send immediately when connected" is unchecked, the first email sent will sit in Outbox permanently. It has a Date of None and is in bold but not italics.
    Any subsequent emails will have a proper datestamp and be in italics and get sent at the appropriate time (so long as the first email is still sitting there blocked).

    I have traced the problem to this line of code

     

    if (_explorer.Selection[1] is Outlook.MailItem)

     

    The cast(?) to MailItem seems to be changing the Selection so that it displays incorrectly. Why is it doing this and how can I work around it?


    Monday, March 29, 2010 5:10 AM

Answers

  • Fixed!

                if (_explorer.Selection[1] is Outlook.MailItem)
     
    becomes

                bool isMailItem = false;

                Object objectToInspect = _explorer.Selection[1];

                Type type = objectToInspect.GetType();

                try
                {
                    if ((Outlook.OlObjectClass)type.InvokeMember("Class", System.Reflection.BindingFlags.GetProperty, null, objectToInspect, null) == Outlook.OlObjectClass.olMail)
                    {
                        isMailItem = true;
                    }
                }
                catch (Exception ex)
                {
                    //eat
                }

                if (isMailItem)

    This doesn't appear to touch the MailItem, so I'm not getting the problem.

    • Marked as answer by 2095 Thursday, April 01, 2010 5:21 AM
    Thursday, April 01, 2010 5:21 AM

All replies

  • Hello,

    Which version of Outlook you are working with? Do you access the first mail in Outbox? How? Where you use this code above? I am afraid that we could not access any mail item when they are in Outbox folder.

    I have made a test in UI. First I uncheck "Send immediately when connected" option. After sending some mail to myself for test, they all stay in Outbox folder as bold and italic ones. When I click Send/Receive button, they all are sent. However, before clicking Send/Receive button, if I open one mail in Outbox folder by double-clicking it, this mail will be changed as a not bold and not italic one. If I open Message option dialog by right-clicking a mail and choosing Message option, it will turn out a bold and not italic one. In these two cases, this item will not be sent by clicking Send/Receive button. I am afraid this behavior is by design.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, March 31, 2010 9:32 AM
  • Hi Bessie,

    >I am afraid that we could not access any mail item when they are in Outbox folder.

    >I am afraid this behavior is by design.

    Yes, I shouldn't touch any of the MailItems in the Outbox. Unfortunately, "is MailItem" does touch them.

     

    All I need the line of code to do is check if what is selected is an email (ie a MailItem). If it is, we change the Context menu to add some options from our add-in. If it isn't an email, we don't.

     

    Is there a way to find out the type of the Object in _explorer.Selection[1] without using a cast or an is?

    If if use

     

    Type type = _explorer.Selection[1].GetType()


    I just get     System.__ComObject

    which is not very useful. I want to know what Type it is inside the wrapper.

     

    I have tried

                Object objectToInspect = _explorer.Selection[1];

      
                Type type = objectToInspect.GetType();

                Type realType = (Type)type.InvokeMember("GetType", System.Reflection.BindingFlags.GetProperty, null, objectToInspect, null);

     

    but I get a runtime error NAME

    What is wrong with this bit of code.?

     

    >Which version of Outlook you are working with? Do you access the first mail in Outbox? How? Where you use this code above?

    2003 SP3. No. Command bars update handler.

     

     

    Thursday, April 01, 2010 1:34 AM
  • Fixed!

                if (_explorer.Selection[1] is Outlook.MailItem)
     
    becomes

                bool isMailItem = false;

                Object objectToInspect = _explorer.Selection[1];

                Type type = objectToInspect.GetType();

                try
                {
                    if ((Outlook.OlObjectClass)type.InvokeMember("Class", System.Reflection.BindingFlags.GetProperty, null, objectToInspect, null) == Outlook.OlObjectClass.olMail)
                    {
                        isMailItem = true;
                    }
                }
                catch (Exception ex)
                {
                    //eat
                }

                if (isMailItem)

    This doesn't appear to touch the MailItem, so I'm not getting the problem.

    • Marked as answer by 2095 Thursday, April 01, 2010 5:21 AM
    Thursday, April 01, 2010 5:21 AM
  • Hello,

    I'm glad to see that you have been worked it out. Thank you very much for taking a moment to come back and report this. I'm sure this will help others in the future.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, April 01, 2010 5:26 AM