简体   繁体   English

如何使用 C# 更新所有 Outlook(2003) 文件夹

[英]how to Update All Outlook(2003) Folders using C#

I have to update all the Outlook Folders from my application.我必须从我的应用程序中更新所有 Outlook 文件夹。 Here is my code, but it doesn't work.这是我的代码,但它不起作用。

Outlook.Application app = null;
Outlook.SyncObject _syncObj = null;

while (OutlookNotFound)
{
    i++;
    try
    {
        app = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
        OutlookNotFound = false;
    }
    catch (System.Exception ex)
    {
        Thread.Sleep(1000);
    }
    finally
    {
        if (i > 10)
        OutlookNotFound = false;
    }
}

if (!OutlookNotFound)
{
    //app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.);
    if (app != null)
    {
        for (Int32 j = 0; j < app.Session.SyncObjects.Count; j++)
        {
            _syncObj = app.Session.SyncObjects[1];
            _syncObj.SyncEnd += 
                new Outlook.SyncObjectEvents_SyncEndEventHandler(_syncObj_SyncEnd);
        }

        _syncObj.Start();
        //app.Session.SyncObjects.AppFolders.SyncEnd += 
        //    new Outlook.SyncObjectEvents_SyncEndEventHandler(AppFolders_SyncEnd);
        //app.Session.SyncObjects.AppFolders.Start();

        return true;
    }
    else
    {
        return false;
    }
}

it's a bit of an usual approach of trying to 'grab' the active outlook object... Especially, if there isn't an active object.这是试图“抓住”活动的 outlook object 的一种常用方法......特别是,如果没有活动的 object。 A more standard approach is something to the effect of:更标准的方法是:

outlookApplication = new Application();
outlookNamespace = m_OutlookApplication.GetNamespace("mapi");

// If an outlook app is already open, then it will reuse that
// session. Else it will perform a fresh logon.
outlookNamespace.Logon(accountName, password, true, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM