【发布时间】: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