C# SyncObject with Outlook Interop

1

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:

  1. Is SyncObject the right mechanism I should be using to sync this outlook folder with the imap server?
  2. 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?

  3. 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");
    }
    
c#
interop
outlook
asked on Stack Overflow Dec 22, 2011 by scadav • edited Dec 22, 2011 by Didier Ghys

1 Answer

3

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();

answered on Stack Overflow Feb 21, 2012 by BugFinder • edited Oct 16, 2013 by Xaruth

User contributions licensed under CC BY-SA 3.0