Unable to cast COM object of type 'System.__ComObject' to interface type 'Redemption.RDOAppointmentItem'

62 Views Asked by At

The code below works fine on five PC's but it's failing on one. I had the same user login to a VM and the code ran fine. It's failing at the foreach loop where I cast "RDOAppointmentItem item in calendar.Items".

Here's the exception returned:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Redemption.RDOAppointmentItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{4959BA11-F9D0-4E57-AA7E-125636EEE44A}' failed due to the following error: No such interface supported (0x80004002 (E_NOINTERFACE)).

Am I failing to cast or account for something in my code for this one users machine specific issue?

RDOSession session = null;
RDOFolder calendar = null
 try
 {
    session = new RDOSession();
    session.Logon(System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, true, System.Reflection.Missing.Value, false);

    calendar = session.GetDefaultFolder(rdoDefaultFolders.olFolderCalendar);
 
    foreach (RDOAppointmentItem item in calendar.Items)
    { //do stuff}
2

There are 2 best solutions below

0
Dmitry Streblechenko On BEST ANSWER

It is theoretically possible to have items other than appointments in the Calendar folder. I have a couple ghost items like that - not sure where they came from. You might want to dynamically check if the item is indeed RDOAppointmentItem:

foreach (RDOMail entry in calendar.Items)
{
   if (entry is RDOAppointmentItem item)
   {
       //do stuff
   } 
}
1
Eugene Astafiev On

Are you sure that all items in the Outlook folder appointments?

Outlook folders may contain different kind of items - documents, mails, notes and etc. The calendar folder doesn't guarantee that all items strictly will be of the appointment type. It just have the default item type set to the appointment message class, so by default when creating a new item you will get an appointment item. But you may keep any kind of items in any folder in Outlook.

You can check the MessageClass property at runtime to make sure that you deal with a particular item class. Or just rely on the .Net runtime as Dmitry noted:

if (entry is RDOAppointmentItem item) then ...