【问题标题】:Best way to find all Outlook calendars in all accounts在所有帐户中查找所有 Outlook 日历的最佳方法
【发布时间】:2016-09-28 03:14:38
【问题描述】:

我有一个使用 COM 接口与 Outlook 通信的应用程序。

我有一个下拉框,其中包含所有帐户的所有日历。

有一些报告的情况是,在使用 Exchange 和 Outlook 2010 时,应用程序在加载日历列表时会挂起。会不会是代码(如下所示)或防病毒阻止了地址信息的访问?

某些 Exchange 帐户随机失败。

private void AppLoad(object sender, EventArgs e)
{
    Outlook.Application msOutlook = new Outlook.Application();
    Outlook.NameSpace session = msOutlook.Session;
    Outlook.Stores stores = session.Stores;

    foreach (Outlook.MAPIFolder folder in session.Folders)
    {
        GetFolders(folder, msOutlook);
    }

    if(calendarSelector.Items.Count==0)
    {
        StoreAndCalendar ci = new StoreAndCalendar();
        ci.Text = "Default";
        ci.Account = "Noaccount";
        ci.Storename = "Noaccount";
        ci.Value = "Noaccount";
        calendarSelector.Items.Add(ci);
    }
    else
    {
        try
        {
            calendarSelector.SelectedIndex = 0;
        }
        catch { }
    }

    //preselect default calendar and account
    GetDefaultCalendarAndAccount();
}

public void GetFolders(Outlook.MAPIFolder folder, Outlook.Application app)
{
        if (folder.Folders.Count == 0)
        {
            if (folder.DefaultItemType == Outlook.OlItemType.olAppointmentItem)
            {
                StoreAndCalendar ci = new StoreAndCalendar();
                Outlook.Account acc = GetAccountForFolder(folder, app);
                if (acc != null)
                {
                    if (!folder.FullFolderPath.Contains("Deleted"))
                    {
                        ci.Text = folder.Name + " - " + acc.DisplayName;
                        ci.Account = acc.DisplayName;
                        ci.Storename = acc.UserName;
                        ci.Value = folder.Name;
                        calendarSelector.Items.Add(ci);
                    }
                }
            }
        }
        else
        {
            if (folder.DefaultItemType == Outlook.OlItemType.olAppointmentItem)
            {
                StoreAndCalendar ci = new StoreAndCalendar();
                Outlook.Account acc = GetAccountForFolder(folder, app);
                if (acc != null)
                {
                    if (!folder.FullFolderPath.Contains("Deleted"))
                    {
                        ci.Text = folder.Name + " - " + acc.DisplayName;
                        ci.Account = acc.DisplayName;
                        ci.Storename = acc.UserName;
                        ci.Value = folder.Name;
                        calendarSelector.Items.Add(ci);
                    }
                }
            }
            foreach (Outlook.MAPIFolder subFolder in folder.Folders)
            {
                GetFolders(subFolder, app);
            }
        }
}

Outlook.Account GetAccountForFolder(Outlook.MAPIFolder folder, Outlook.Application app)
{
        // Obtain the store on which the folder resides.
        Outlook.Store store = folder.Store;

        // Enumerate the accounts defined for the session.
        foreach (Outlook.Account account in app.Session.Accounts)
        {
            // Match the DefaultStore.StoreID of the account
            // with the Store.StoreID for the currect folder.
            if (account.DeliveryStore.StoreID == store.StoreID)
            {
                // Return the account whose default delivery store
                // matches the store of the given folder.
                return account;
            }
        }
        // No account matches, so return null.
        return null;
}


public void GetDefaultCalendarAndAccount()
{
        try
        {
            Outlook.Application OutlookApp = new Outlook.Application();
            Outlook.NameSpace ns;
            Outlook.MAPIFolder defaultfolder;
            Outlook.Account defaultaccount;
            ns = OutlookApp.Session;
            ns.SendAndReceive(false);
            defaultfolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
            defaultaccount = GetAccountForFolder(defaultfolder, OutlookApp);
            String defaultaccandfold = defaultfolder.Name + " - " + defaultaccount.DisplayName;
            int index = 0;
            for (int i = 0; i < calendarSelector.Items.Count; i++)
            {
                string value = calendarSelector.GetItemText(calendarSelector.Items[i]);
                if (value == defaultaccandfold)
                {
                    calendarSelector.SelectedIndex = index;
                }
                index++;
            }
        }
        catch { }
}

浏览帐户及其所有子文件夹并列出所有有效日历的最佳方式是什么?

另外,选择默认日历:必须这样做吗?

谢谢!

【问题讨论】:

  • 挂在哪一行?你试过调试你的代码吗?
  • 无法调试它,因为它不在开发者机器上。它只是挂起,但如果你关闭 Outlook,它会在组合框中加载日历,这很奇怪。
  • 如果您无法在调试器下运行,请将日志记录添加到您的代码中。

标签: vba outlook calendar outlook-addin outlook-2010


【解决方案1】:

您正在寻找最佳方法。但一些建议:

  • 无需遍历 Accounts,只需解析 Stores
  • 不要处理非电子邮件应用商店对象的应用商店,例如 SharePoint 列表和 Internet 日历(除非您需要这些类型的日历)
  • 不要在 Outlook 对象模型中使用 foreach 循环,使用 for 循环,这样您就可以在每个引用 Outlook 对象的变量上显式调用 Marshal.ReleaseComObject
  • 您对 NameSpace.SendAndReceive 的调用可能是不必要的,可能会导致一些延迟

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多