使用.ActiveInspector()。CurrentItem

时间:2019-07-02 11:46:17

标签: c# outlook vsto outlook-addin

我们在编写VSTO Outlook加载项时遇到了一个问题。在执行ItemSend时,获取组成电子邮件的主题可以很好地工作,但是在撰写电子邮件时(在ItemSend之前)尝试获取主题时,检索到的主题有时为null。它的预览功能是通过功能区上的按钮触发的。

设置一个断点,看起来好像是ActiveInspector()。CurrentItem已经没有提供正确的主题值


Ribbon_TabNewMailMessage.cs:

    private void PreviewButton_Click(object sender, RibbonControlEventArgs e)
        {

            // pointing to ThisAddIn.cs (see code block below)
        if (Globals.ThisAddIn.Application.ActiveInspector() != null)
            {
                // Obviously sometimes not catching subject
                Outlook.MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;

                // BAD CASE: mailItem.Subject is sometimes NULL
                var aSubj = mailItem.Subject;


ThisAddIn.cs:

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

            try
            {

                ...
                // Get the Application object
                Outlook.Application application = this.Application;

                // Subscribe to the ItemSend event, that it's triggered when an email is sent
                application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(ItemSend_BeforeSend);

                // new itemsend event
                void ItemSend_BeforeSend(object item, ref bool cancel)
                {

                   Outlook.MailItem mailItem = (Outlook.MailItem)item;

           // GOOD CASE: this one is working properly !!!   
           var bSubj = mailItem.Subject;

mailItem.Subject应该具有邮件主题的值,但是在错误的情况下,它返回NULL。

2 个答案:

答案 0 :(得分:0)

Subject属性只有在焦点从“主题”编辑框移开后才会更新。

您可以尝试找到编辑框HWND并使用Windows API检索其文本(GetWindowText等)。

答案 1 :(得分:0)

Save方法可以帮助您获取最新更改。它将Microsoft Outlook项目保存到当前文件夹,或者如果是新项目,则保存到该项目类型的Outlook默认文件夹。

还可以将光标切换到窗口中的另一个字段,以将更改传播到Outlook对象模型。 Outlook缓存值,直到光标移动到另一个字段。在处理OOM时,这是一个已知问题。