如何用C#在Outlook自动化中指向正确的存储区? [英] How to point to the correct Store in Outlook automation by C#?

查看:0
本文介绍了如何用C#在Outlook自动化中指向正确的存储区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多VBA自动化链接Outlook和Word解决方案;这很好,但时间是不可阻挡的……所以,我开始装饰和扩展旧的解决方案,用C#/VS2017包装它。 通过传统的Winform,我可以选择我的患者,从这个操作中我可以执行很多操作,包括打开正确的Outlook联系人;这就是问题所在,因为我无法获得正确的Store;Patients.pst根据机器的不同,可能是第一、第二、第三…… 在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.");
        }
    }
不幸的是,它总是指向同一家第一商店,没有病人的那家…… 如果我更改了商店顺序,我可以跳过这一步并测试其他东西,但这当然不是正确的方法。

还有没有其他人的头/眼睛能看到光?

TIA

推荐答案

坐着写问题时,看着一只黄色的橡皮鸭--以及我一岁女儿的许多其他东西;),我意识到WhichStore.Session.GetDefaultFold在这种情况下有点奇怪。我只更改了这个

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

至:

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

瞧!神奇的事情也发生在C#上! 会话returns the default NameSpace object for the current session

ps:黄色橡皮鸭;务实程序员的家伙真的知道一些秘密和诀窍;)

谢谢托马斯和亨特!

这篇关于如何用C#在Outlook自动化中指向正确的存储区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆