C# Interface into Microsoft Outlook Calendar?

R

Rico

From a C# program, how do I access my own Microsoft Outlook Calendar?

My ultimate goal is to add the information to my screen saver to
notify other employees where I am when not at my desk.

I assume the answer has something to do with an API (Application
Programmer Interface), but I've never used this one before.

Thanks.
 
R

Rico

Starting with this link:

http://stackoverflow.com/questions/90899/net-get-all-outlook-calendar-items

I tried the following code:

------------------------------- C U T - H E R E
-------------------------------

Microsoft.Office.Interop.Outlook.Application oApp = null;
Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null;
Microsoft.Office.Interop.Outlook.MAPIFolder calendarFolder = null;

oApp = new Microsoft.Office.Interop.Outlook.Application();
mapiNamespace = oApp.GetNamespace("MAPI");
calendarFolder = mapiNamespace.GetDefaultFolder
(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);

foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in
calendarFolder.Items)
{
System.Console.WriteLine(item.Location);
}

------------------------------- C U T - H E R E
-------------------------------

But get an exception at the start of the foreach loop:

System.InvalidCastException was unhandled

Message="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: No such interface supported
(Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

------------------------------- C U T - H E R E
-------------------------------

For whatever it's worth, I did put into AsssmelbyInfo.cs:

[assembly: ComVisible(true)]

but I still get the exception. Why?

Any ideas, please?
 
R

Rico

Starting with this link:

http://stackoverflow.com/questions/90899/net-get-all-outlook-calendar...

I tried the following code:

---------------- C U T - H E R E ----------------

Microsoft.Office.Interop.Outlook.Application oApp = null;
Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null;
Microsoft.Office.Interop.Outlook.MAPIFolder calendarFolder = null;


oApp = new Microsoft.Office.Interop.Outlook.Application();
mapiNamespace = oApp.GetNamespace("MAPI");
calendarFolder = mapiNamespace.GetDefaultFolder
(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);


foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in
calendarFolder.Items)
{
System.Console.WriteLine(item.Location);
}


---------------- C U T - H E R E ----------------

But get an exception at the start of the foreach loop:

System.InvalidCastException was unhandled

Message="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: No such interface supported
(Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

---------------- C U T - H E R E ----------------

For what it's worth, I did put into AsssmelbyInfo.cs:

[assembly: ComVisible(true)]

but I still get the exception. Why?

Any ideas, please?
 
P

Peter Duniho

[...]
But get an exception at the start of the foreach loop:

System.InvalidCastException was unhandled

Message="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: No such interface supported
(Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

A quick Google search suggests a number of possible explanations:

-- Wrong version of Office
-- Improper use of the Outlook process (one message suggests that if
you don't explicitly call Quit() on your Outlook application object, you
might have problems with your code on subsequent executions)
-- An item in the calender that's not actually an AppointmentItem (so
either iterate over objects and move the cast into the loop where you can
check the type, or use one of the LINQ idioms that filters by type)

Surely these are not the only possibilities, but you might as well
investigate them.

I also found a recommendation for the
microsoft.public.office.developer.outlook.vba newsgroup, which would be
one of those "more appropriate" newsgroups I mentioned before. Not sure
if that's a better or worse choice than the m.p.outlook.interop newsgroup
you found on your own.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top