Unable to cast COM object of type 'System.__ComObject' error

Hello all,

I have created an add-in which adds user properties to appointments. When I try to recover these user properties from the calendar of other users, I use the find method to recover all the appointments between a period of time of 3 months. Then, I browse the results to find if the properties meet the criteria, I have the following error :

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.AppointmentItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063033-0000-0000-C000-000000000046}' failed due to the following error: Not such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

This error appears unpredictably. But when I restart Outlook, it appears on different appointments.

My code is as following :

Outlook.Items appointements = calendar.Items;

object item = appointements.Find(this._searchFilter);
while (item != null)
{
try
{
Outlook.AppointmentItem appointment = (Outlook.AppointmentItem)item;
results.Add(appointment);
}
catch (Exception ex)
{
}
item = appointements.FindNext();
}

The error is raised on the following line : Outlook.AppointmentItem appointment = (Outlook.AppointmentItem)item;

Thank you very much. I am a bit desperate

Remi



Answer this question

Unable to cast COM object of type 'System.__ComObject' error

  • aleycris

    Hello Remi,

    Tip,

    if you already add userproperties to your appointments, then you can modify your filter to get only this appointments that match with a userproperty.

    This speeds up everything, because you must not iterate over items that are normal claendar items and you will only get your appointments.

    Hope this helps,
    greets, Helmut
    [http://www.x4u.de]



  • iambic

    Hi all,

    Just incase anyone else is getting this problem. Remember to call the Microsoft.Office.Interop.Outlook.Application.Quit() method (or Outlook.Application.Quit()) on a Dispose() method.

    This may solve you problem (it did mine)

     

    - Brent


  • DStudent

    Hello Remi,

    maybe try this to see what type of item raises the error.

    Outlook.Items appointements = calendar.Items;

    object item = appointements.Find(this._searchFilter);
    while (item != null)
    {
    try
    {
    Outlook.AppointmentItem appointment = (Outlook.AppointmentItem)item;
    results.Add(appointment);
    }
    catch (Exception ex)
    {

    string messageClass = (string) (string)item.GetType().InvokeMember ("MessageClass", System.Reflection.BindingFlags.GetProperty, null, item, null);

    MessageBox.Show("Item is of Type: " + messageClass );
    }
    item = appointements.FindNext();
    }

    You also can retrieve the Subject Property this way and see wich Item raises the error.
    Maybe try this and give us a feedback of the messageclass of the bad item.

    Hope this helps,
    greets, Helmut



  • Bill Gerold

    I've discovered something new. When I connect to Outlook, a process OUTLOOK.EXE appears in Task manager. This process exits whenever my .NET application exits.

    However, my application just started to get unusable Appointments again. I found that after exiting my application, OUTLOOK.EXE remained as a task! When restarting my application, the same problem happened.

    When I manually killed OUTLOOK.EXE and restarted my application, everything was working fine again!

    Interesting..
    Walter


  • Bose

    Hey,

    I don't know a lot about the Outlook OM or MAPI. But my understanding is as follows:

    The Calender is  MAPI folder and a MAPI folder can contain different types of item. It seems like you should never assume that all items in you Calender folder will be AppointmentItems. Don't ask me why now, coz I don't know :-)

    Keeping that in mind, the InvalidCastException you are getting is a expected. You code should simply ignore the exception and continue. The code in your original posting was already doing this.

    I would suggest you ignore the exceptions and verify your addin is doing what you expect. You can rewrite the code as follows to avoid throwing any exception (logic of the code remains unaffected):

    while (item != null)
    {
            //    "as" keyword performs a cast but returns null if cast fails
            Outlook.AppointmentItem appointment = item as Outlook.AppointmentItem;
           
            if (appointment != null)
            {
                results.Add(appointment);
            }
            item = appointements.FindNext();
    }

    So, as long as you can verify that your addin is performing as you expect you are fine. However, if you think that cast on valid AppointmentItems is failing then thats another question.

    Please update us if this this helps. Thanks.

    P.S. You may want to try out Helmut's suggestion. It may be more efficient than looping through all the items



  • Doogshnooglis

    Thank you very much. It seems to work. I'll test it a bit more and give you news.

    Remi


  • flashfreaker

    Hi,

    You might want to try posting this query to the Outlook Developer Forums, there you will be able to get a quicker response as it is related to Outlook Object Model.

    http://msdn.microsoft.com/newsgroups/default.aspx dg=microsoft.public.office.developer.outlook.vba&lang=en&cr=US

    Thanks.


  • nredman

    Hi,

    This means that the item is not an AppointmentItem object. It is most likely one of the other Outlook item objects e.g. MeetingRequestItem.

    I am not sure exactly what type of item it is, but the following code should tell you:

    while (item != null)
    {
        // as keyword casts but it does not throw exception if cast fails
        Outlook.AppointmentItem appointment = item as Outlook.AppointmentItem;

        // Error checking
        if (appointment == null)
       
    {
           
    string itemType = "";

            if ((item as Outlook.MailItem) != null) itemType = "MailItem";
           
    else if ((item as Outlook.ContactItem) != null) itemType = "ContactItem";
           
    else if ((item as Outlook.JournalItem) != null) itemType = "JournalItem";
           
    else if ((item as Outlook.MeetingItem) != null) itemType = "MeetingItem";
           
    else if ((item as Outlook.NoteItem) != null) itemType = "NoteItem";
           
    else if ((item as Outlook.PostItem) != null) itemType = "PostItem";
           
    else if ((item as Outlook.ReportItem) != null) itemType = "ReportItem";
           
    else if ((item as Outlook.RemoteItem) != null) itemType = "RemoteItem";
           
    else if ((item as Outlook.TaskItem) != null) itemType = "TaskItem";
           
    else if ((item as Outlook.TaskRequestItem) != null) itemType = "TaskRequestItem";
           
    else if ((item as Outlook.TaskRequestAcceptItem) != null) itemType = "TaskRequestAcceptItem";
           
    else if ((item as Outlook.TaskRequestDeclineItem) != null) itemType = "TaskRequestDeclineItem";
           
    else if ((item as Outlook.DistListItem) != null) itemType = "DistListItem";
           
    else if ((item as Outlook.DocumentItem) != null) itemType = "DocumentItem";
           
    else itemType = "Some Other Item";
           
    MessageBox.Show("item type: " + itemType);
        }

        results.Add(appointment);
        item = appointements.FindNext();
    }

    From here on you can investigate why you are recieving an unexpected type of item. Please post here again with the result of your investigation.

    Thanks



  • Peter Sollich

    Thanks for your help.

    I tried your solution and when an exception is raised, the messagebox shows the following message : Item is of Type IPM.Appointment.

    So, as you can see, normally there is no problem to cast the objet to the type Outlook.AppointmentItem. But it's not the case.

    Maybe it's due to timeouts or something else.

    I want to say again that the exception is not raised on the same appointment from one time to another. But when I perform a search and that an exception is raised, this exception will be raised on the same appointments if I restart the search. To prevent the problem, I have to close Outlook and start it again. If you don't understand what I am saying, please tell me.

    Thank you very much.

    Remi


  • viliescu

    Another thing I want to add is that when I use the invokemember, I can recover the information, but is there another way that is more "conventional" :-).

    Remi


  • lew26

    Hello all,

    sorry to bump this old thread, but I am running into what seems to be EXACTLY the same problem.

    I'm not so sure it is related to the Outlook DOM. In fact, I suspect this not to be a n API problem but an Outlook bug. I hope this forum is the right place for this.

    I am carrying out a similar task as the original poster: I am retrieving AppointmentItems, ContactItems and TaskItems from Outlook. The only difference is I am using Outlook's AdvancedSearch so I can use complex filtering rules.

    Unfortunately, sometimes - apparently after Outlook has been running for longer - Outlook no longer returns AppointmentItems. It seems to return a number of "System.__ComObject" instead. These are not sporadic items between valid the AppointmentItems - from a certain point, I cannot get any AppointmentItem at all, although the result count remains the same as before. So, in short, the appointments seem to be there, seem to be in the result set, but they get returned as COM objects of unknown type and cannot be cast to AppointmentItem.

    I've tried to cast the objects to any Outlook ItemType I could think of without any luck. When I use InvokeMember (from a suggestion above) like this:

    string messageClass = (string) (string) item.GetType().InvokeMember("MessageClass", System.Reflection.BindingFlags.GetProperty, null, item, null);

    I get "IPM.Appointment". However, I have no success doing anything at all with the item, because I cannot cast it to AppointmentItem (or _AppointmentItem for that matter).

    Any ideas about this I'm tempted to just kill the Outlook process whenever this error occurs. Strangely enough - Closing Outlook and restarting it, seems to do the trick... I get my AppointmentItems correctly again! It is hard for me to recreate this problem, it seems I have to test for most of the day before it happens. Could it be that I need to release some resources explicitly

    I should add that this only happens with the Calendar folder. I am always able to list TaskItems and ContactItems even when Outlook is in this state. I use exactly the same method for getting these items. They all get returned as you would expect, except for appointments.

    I am working with C# / .NET framework 2.0. My Outlook version is: 2003 (11.8010.8107) SP2. I have not been able to test this on other versions, though I will try.

    Thanks for any input about this.

    Kind regards,

    Walter


  • Adnan Siddiqi

    I tried your solution and I still had the same problem. I receive the following message : "Unexpected item type".

    The thing is that when I start again the search, the error fires on different appointments. I really don't understand why.

    Thank you

    Remi


  • Unable to cast COM object of type 'System.__ComObject' error