I am hoping someone can help me. Let me first state that I am a very amateur programmer.
I have an IMAP email account within outlook. I want to take a single email folder within that account and ensure that all the messages in that folder within outlook are in sync with what is on the IMAP email server programmatically. I created the code down below, but I am having the following issues/questions:
If I run it 10 times, it will run through without errors 2 or 3 times, but then it will error out on this line:
sync = app.Session.SyncObjects[folder];
with the following error:
"System.Runtime.InteropServices.COMException (0x80020005): Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))\r\n at Microsoft.Office.Interop.Outlook.SyncObjects.get_Item(Object Index)\r\n at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\DKS\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 38"
Any ideas about this error?
When I do run it, whether it runs successfully or not, it always leave the outlook.exe process running on my machine. How do I close interop out properly?
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MAPIFolder folder = null;
Microsoft.Office.Interop.Outlook.SyncObject sync = null;
try
{
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
folder = ns.Folders["yahoo_imap_mail"].Folders["Trash"];
sync = app.Session.SyncObjects[folder];
sync.Start();
}
catch (System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show(ex.ToString());
Console.WriteLine(ex.ToString());
}
finally
{
ns = null;
app = null;
folder = null;
sync = null;
}
MessageBox.Show("Complete");
}
Better late than never - but. Looking at your code it doesnt match code Ive seen where the parameter for the app.Session.SyncObjects[x] where x is an INTEGER not a folder. Working code I found was similar to
for (Int32 i = 0; i < app.Session.SyncObjects.Count; i++)
{
_syncObj = app.Session.SyncObjects[1];
_syncObj.SyncEnd +=_syncObj_SyncEnd;
}
_syncObj.Start();
on top of that, your app doesnt tell outlook to close. So why would it? You told it to open it. You have also no error trapping - so for whatever reason, what if it cant find the folder? What if it cant connect to outlook for some reason? If you want outlook to end you should tell it to.
With app.Quit();
User contributions licensed under CC BY-SA 3.0