如何通过 C# 指向 Outlook 自动化中的正确存储?



我有很多VBA自动化,可以将Outlook和Word解决方案互连在一起;这很好,但时间是无情的...因此,我开始装饰和扩展旧解决方案,用 C#/VS2017 包装它。 通过传统的 Winform,我可以选择我的患者,从这个动作中我做了很多动作,包括打开正确的 Outlook 联系人;这就是问题所在,因为我无法获得正确的商店;患者.pst 取决于机器,可能是第 1 个、第 2 个、第 3 个...... 在 VBA 中,我这样做:

WhichStoreNameToPointAt="patients"
Set myNamespace = myolApp.GetNamespace("MAPI")
For i = 1 To myNamespace.Stores.Count Step 1
If myNamespace.Stores.item(i).DisplayName = WhichStoreNameToPointAt Then
intOutlookItemStore = i
End if
End If
Set myFolderPatients = myNamespace.Stores.item(intOutlookItemStore).GetDefaultFolder(olFolderContacts)

它总是像一个魅力。

在 C# 中,我尝试了很多变体,但无法指向正确的存储:

public void OpenPatientContact(string patientName)
{
Outlook.Store whichStore = null;
Outlook.NameSpace nameSpace = OlkApp.Session;
int i = 1;
foreach (Outlook.Folder folder in nameSpace.Folders)
{
bool p = false;
if (whichStoreNameToPointAt == folder.Name)
{
p = true;
whichStore = folder.Store;
//Correct Store selected; I can tell because of this watch:
//whichStore.displayname == whichStoreNameToPointAt 
} 
i++;
if (p)
break;
}
var contactItemsOlk = whichStore.Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts).Items;
// The problem is below; always the first Store
Outlook.ContactItem contact = (Outlook.ContactItem)contactItemsOlk
.Find(string.Format("[FullName]='{0}'", patientName)); //[1];
if (contact != null)
{
contact.Display(true);
}
else
{
MessageBox.Show("The contact information was not found.");
}
}

不幸的是,它一直指向同一个第一个商店,那个没有病人的商店...... 如果我更改商店订单,我可以克服这一点并测试其他东西,但这当然不是正确的方法。

还有其他的头/眼睛可以看到光吗?

蒂亚

在坐着写问题时,看着一只黄色的橡皮鸭 - 以及属于我 1 岁女儿;)的许多其他东西,我意识到在这种情况下,哪个商店.会话.GetDefaultFolder 有点奇怪。我只是改变了这个

var contactItemsOlk = whichStore.Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts).Items;

对此:

var contactItemsOlk = whichStore.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderContacts).Items;

瞧!C#也会发生奇迹! 会话返回当前会话的默认命名空间对象。

PS:黄色橡皮鸭;《实用程序员》的家伙们真的知道一些秘密和技巧;)

谢谢托马斯和亨特!

相关内容

最新更新