InvalidCastException System.__ComObject couldnt be casted

N

nickbw2003

Hi,
I'm writing a Application, which imports forms in Microsoft Outlook 2003
according to this example from Microsoft:

http://support.microsoft.com/?scid=kb;en-us;310244&x=17&y=10#

But everytime compiling it, it says, that System.__ComObject couldnt be
casted to Microsoft.Office.Interop.Outlook._ContactItem.

Here is my Code:


using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Outlook;

namespace formImport
{
class Program
{
static void Main(string[] args)
{
String formname;
String filename;

DirectoryInfo dir = new DirectoryInfo(@"c:commonformsdata");
FileInfo[] files = dir.GetFiles("*.oft");

foreach( FileInfo file in files )
{
formname = Path.GetFileNameWithoutExtension(file.FullName);
filename = Path.GetFileName(file.FullName);

_Application olApp = new ApplicationClass();
_NameSpace olNs = olApp.GetNamespace("MAPI");
MAPIFolder oContacts =
olNs.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
_ContactItem oContact =
(_ContactItem)olApp.CreateItemFromTemplate(@"C:commonformsdata\" +
filename,oContacts);
FormDescription oForm = oContact.FormDescription;
oForm.Name = formname;
oForm.PublishForm(OlFormRegistry.olFolderRegistry,
oContacts);
oContact.Close(OlInspectorClose.olSave);
}
Console.ReadLine();
}
}
}


This line produces the error:
_ContactItem oContact =
(_ContactItem)olApp.CreateItemFromTemplate(@"C:commonformsdata\" +
filename,oContacts);

I've no idea how to solve to problem. I googled for hours and found nothing.
I hope you can help me.

Thanks in advance.

Dennis
 
K

Ken Slovak - [MVP - Outlook]

You get the error on compilation and not when running the code?

Do you still get the compilation error if you use the code from the sample
and call to CreateItem() and not CreateItemFromTemplate()? What about if you
fully qualify your object declarations?
 
Top