.NET Framework Bookmark and Share   
 index > Common Language Runtime > Outlook error
 

Outlook error

The below code opens a mail with a file attached on clicking a button say “Open mail� But when you click “Open mail�continuously, it gives this error (like in the fourth or fifth time/click)

<Message> Creating an instance of the COM component with CLSID {0006F03A-0000-0000-C000-000000000046} from the IClassFactory failed due to the following error: 80010108.</Message>

//Get Outlook object.

Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application();

if (outlookObj == null)

{

System.Diagnostics.Process.Start("Outlook.exe");

outlookObj = new Microsoft.Office.Interop.Outlook.Application();

}

Microsoft.Office.Interop.Outlook.MailItem outlookMailItem = (Microsoft.Office.Interop.Outlook.MailItem)outlookObj.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

if (outlookMailItem == null)

{

System.Diagnostics.Process.Start("Outlook.exe");

outlookMailItem = (Microsoft.Office.Interop.Outlook.MailItem)outlookObj.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

}

outlookMailItem.To = "xyz@abc.com";

outlookMailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;

outlookMailItem.Attachments.Add(filePath, (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, 1, null);

outlookMailItem.Display(true);

How to get rid of it ?

Neeti Kapoor
It is a pretty low-level error: 0x80010108 = RPC_E_DISCONNECTED = "The object invoked has disconnected from its clients." In other words, while your program was talking to Outlook, Outlook quit without saying goodbye nicely. That might have something to do with your attempts to start Outlook.exe yourself when you get a null. That's always wrong, just remove that code. Most Office programs are singletons, trying to start one when it is already running will cause the 2nd instance to immediately exit.

Not so sure that fixes anything, the null test is mysterious in the first place. Post to an Office development newsgroup or forum if you still have trouble, after checking that Office is installed properly.

Hans Passant.
nobugz

Actually, outlook will be launched automatically when we create an outlook application in code, but we need to wait several seconds before constructing email, because outlook needs time to complete its initiation work. following code snippet may give some help:

using Outlook = Microsoft.Office.Interop.Outlook;

Outlook.Application app = new Outlook.Application();
Console.WriteLine("starting...");
Thread.Sleep(5000);
Console.WriteLine("started");

Outlook.MailItem eMail = (Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);
eMail.Subject = "subject";
eMail.To = "Eric Yang (CS&S)";
eMail.Body = "test for test";

OpenFileDialog attachment = new OpenFileDialog();
attachment.Title = "Select a file to send";
attachment.ShowDialog();
if (attachment.FileName.Length > 0)
{
eMail.Attachments.Add(
attachment.FileName,
Outlook.OlAttachmentType.olByValue,
1,
attachment.FileName);
}

eMail.Importance = Outlook.OlImportance.olImportanceLow;

((Outlook._MailItem)eMail).Display(false);

http://msdn.microsoft.com/en-us/library/ms268731(VS.80).aspxprovides more outlook automation samples.

Thanks,
Eric


Please remember to mark helpful replies as answers and unmark them if they provide no help.
eryang
So you mean, Outlook.Application object can never be null ! So the code to start Outlook.exe shouldn’t be there. What if the system doesn’t have Outlook installed !! I need to know the exact exception it throws or the message.
Neeti Kapoor
Hi,
Above code works well on machine which has outlook, but I haven't test it without outlook installed.
You are right, if outlook is not installed, the Outlook.Application object maybe null or there maybe an exception, so there are some ideas for you, hope it can help:
1. If outlook is necessary for the application, we can set outlook as the prerequisite when deploy application on end users' machine.
2. If outlook is optional for the application, before sending mail, we need to check whether outlook is installed, following links show how to detect it:
http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/00415957-f7d9-4986-aa36-bb61a8146045;
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/a337b5b7-c87f-4bb9-b808-6f7294379c69

Thanks,
Eric
Please remember to mark helpful replies as answers and unmark them if they provide no help.
eryang

You can use google to search for other answers

Custom Search

More Threads

• Hashing Passwords on Different Machines
• AccessViolation in multi-threaded interop
• COM Interop Question: Declaring COM type in .NET interface
• Size limit on string returned via PInvoke?
• Extension Properties
• what is shim?
• permissions error when running a console app
• How to trace strong references to object instances?
• Marshal.StructureToPtr
• DllImport problem