Unable to open mailItem because of outlook interface..!

1.7k views Asked by At

I have an application where i create a mailItem using Outlook interop. On some systems the code works without problems.

But on one of the systems this error appears:

Message= Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed

due to the following error: Interface not registered

I think it has something to do with the register see: Answer on Error accesing COM components

But i need to solve this problem in the code, because i can't acces all the systems with this kind of problem.

using Outlook = Microsoft.Office.Interop.Outlook;

//Create email body with the customers
string mailBody = customers;
//Create the email with the settings
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = mailSubject;
mailItem.Attachments.Add(totalPath);
mailItem.Body = mailBody;
mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
try
{
    //Try to open outlook, set message if its not possible to open outlook
    mailItem.Display(true);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
    return false;
}

How can i solve this in my code?

P.S. Every system uses the office 2013 version!

1

There are 1 answers

0
Eugene Astafiev On BEST ANSWER

Try to use the following code instead:

 oApp = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application")) as Microsoft.Office.Interop.Outlook.Application;

It looks like something is wrong with the windows registry records. Take a look at the similar forum thread - Error: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'..

Do you have the Click2Run edition of Office installed on the PC? See How to: Verify Whether Outlook Is a Click-to-Run Application on a Computer for more information.