Having problem in Microsoft Outlook Automation
hi guys,
I am working on a windows service in which i'm trying to automate outlook to send emails periodically based on some criteria.
I developed and tested the code in a console application first and it worked fine. But when i migrated from console to windows service it started misbehaving. Its giving various exception as:
1) Here is the exception that I get when running the service. Service cannot be started. System.Runtime.InteropServices.COMException (0x80020009): Cannot complete the operation. You are not connected. at Microsoft.Office.Interop.Outlook.NameSpaceClass.GetDefaultFolder(OlDefaultFolders FolderType) at ClassLibrary.BaseService.SortInbox() in C:\Users\Neemus\Documents\Visual Studio 2008\Projects\ClassLibrary\ClassLibrary\BaseService.cs:line 48 at ClassLibrary.BaseService.Update() in C:\Users\Neemus\Documents\Visual Studio 2008\Projects\ClassLibrary\ClassLibrary\BaseService.cs:line 311 at ServiceAtBase.UpdateService.OnStart(String[] args) in C:\Users\Neemus\Documents\Visual Studio 2008\Projects\ServiceAtBase\ServiceAtBase\UpdateService.cs:line 24 at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
2)Microsoft Outlook has encountered an error and needs to close.
3)outlookObj.AnserWizard threw an exception of type 'System.Runtime.InteropServices.COMException'
Here is my code:
bool isOutlookRunning = false;
Process[] process = null;
process = Process.GetProcessesByName("OUTLOOK");
if (process.Length > 0)
isOutlookRunning = true;
try
{
obj2 = new Microsoft.Office.Interop.Outlook.Application();
}
catch
{
if (isOutlookRunning)
{
foreach (Process pr in process)
{
if (pr.ProcessName.Contains("OUTLOOK"))
{
pr.CloseMainWindow();
}
}
}
else
Process.Start("OUTLOOK");
obj2 = new Microsoft.Office.Interop.Outlook.Application();
}
ns = obj2.GetNamespace("MAPI");
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
outboxFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
process = null;These errors are not fixed as well.
At one time one exception will pop up and other time some other.
Any help will appreciated...
Thanks
Sunil Sachdeva- SpostatoHarry ZhuMSFTmartedì 1 dicembre 2009 6.55 (From:Visual C# General)
Tutte le risposte
Hi,
For discussions relating to outlook developing, I recommend you create a new thread at :
http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.outlook.interop&cat=en_US_8d5edff5-fda4-4337-a87c-5f7a024ea015&lang=en&cr=US
You will get better and quicker responses from the experts there.
Best regards,
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Thank you very much for the help.Can u tell me where can i find answers for windows service related questions.
Sunil Sachdeva
Sunil Sachdeva - Hi,
You can try : Where is the forum for ... forum:
http://social.microsoft.com/Forums/en-US/whatforum/threads
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.