Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

c# - Outlook VSTO - ActiveInspector returns the incorrect email window's information

I am trying to get the information from an email that I previously opened (by doubleclicking) in outlook. The code works fine until I open multiple emails. What I am finding is that when I click on an email, the inspector activates, but I am getting the information from the last active window, not the current one that I clicked on.

Here is my code:

using Outlook = Microsoft.Office.Interop.Outlook;
using System.Diagnostics;
using System.Threading;

namespace OutlookWindowActivateTest
{
    public partial class ThisAddIn
    {
        //Outlook.Inspectors OurInspectors;
        Outlook.Inspector ActiveInspector;
        Outlook.MailItem ActiveMailItem;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Debug.WriteLine("LOADING CODE");
            Application.Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Mail_Debugger);
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Note: Outlook no longer raises this event. If you have code that 
            //    must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
        }

        public void Mail_Debugger(Outlook.Inspector Inspector)
        {
            Debug.WriteLine("###START DEBUGS VOID MAIL INSPECTOR###");
            // This will fire when an email item is opened in its own window WILL NOT FIRE if you just preview
            if (Inspector.CurrentItem is Outlook.MailItem)
            {
                // this part is correct when I doubleclick an email to open it
                Outlook.MailItem OpenedMailItem = Inspector.CurrentItem as Outlook.MailItem;
                Debug.WriteLine($"  Email doubleclicked -> MailItem details:");
                Debug.WriteLine($"      Subject: {OpenedMailItem.Subject}");
                Debug.WriteLine($"      To: {OpenedMailItem.To}");
                Debug.WriteLine($"      From name: {OpenedMailItem.Sender.Name}");
                Debug.WriteLine($"      From email: {OpenedMailItem.Sender.Address}");
                Debug.WriteLine($"      Conversation ID: {OpenedMailItem.ConversationID}");
                Debug.WriteLine($"  Opened email is {OpenedMailItem.Subject}");
                ((Outlook.InspectorEvents_Event)Inspector).Activate += new Outlook.InspectorEvents_ActivateEventHandler(InspectorActivate);
                ((Outlook.InspectorEvents_Event)Inspector).Deactivate += new Outlook.InspectorEvents_DeactivateEventHandler(InspectorDeactivate);
                ((Outlook.InspectorEvents_Event)Inspector).Close += new Outlook.InspectorEvents_CloseEventHandler(InspectorClose);
                Inspector.PageChange += new Outlook.InspectorEvents_10_PageChangeEventHandler(InspectorPageChange);
            }
            Debug.WriteLine($"  Number of inspectors is {Application.Inspectors.Count}");
            Debug.WriteLine("       Inspector info:");
            foreach (Outlook.Inspector insp in Application.Inspectors)
            {
                Debug.WriteLine($"      Inspector is {insp.Caption}");
            }
            Debug.WriteLine("###END DEBUGS VOID MAIL INSPECTOR###");
        }

        private void InspectorPageChange(ref string page)
        {
            // this also happens when you doubleclick an email in outlook to open it
            Debug.WriteLine("Inspector page has changed");
            Debug.WriteLine($"      {page}");

        }
        private void InspectorClose()
        {
            Debug.WriteLine("Inspector has closed");
        }

        private void InspectorActivate()
        {
            Debug.WriteLine("Inspector was activated by someone clicking on an outlook window that is NOT the base window");
            ActiveInspector = Application.ActiveInspector();
            if (ActiveInspector is null)
            {
                Debug.WriteLine("The inspector is null because it is not ready....");
            }
            else
            {
                Debug.WriteLine($"Caption is {ActiveInspector.Caption}");
                object selectedItem = ActiveInspector.CurrentItem;
                if (selectedItem != null)
                {
                    if (selectedItem is Outlook.MailItem)
                    {
                        ActiveMailItem = selectedItem as Outlook.MailItem;
                        Debug.WriteLine($"ACTIVE INSPECTOR -> MailItem details:");
                        Debug.WriteLine($"      Subject: {ActiveMailItem.Subject}");
                        Debug.WriteLine($"      To: {ActiveMailItem.To}");
                        Debug.WriteLine($"      From name: {ActiveMailItem.Sender.Name}");
                        Debug.WriteLine($"      From email: {ActiveMailItem.Sender.Address}");
                        Debug.WriteLine($"      Conversation ID: {ActiveMailItem.ConversationID}");
                    }
                }
            }
                
        }

        private void InspectorDeactivate()
        {

            Debug.WriteLine("Inspector was deactivated");
            //if (ActiveMailItem != null)
            //{
            //    Debug.WriteLine($"  INSPECTOR -> OLD MailItem details:");
            //    Debug.WriteLine($"      Subject: {ActiveMailItem.Subject}");
            //    Debug.WriteLine($"      To: {ActiveMailItem.To}");
            //    Debug.WriteLine($"      From name: {ActiveMailItem.Sender.Name}");
            //    Debug.WriteLine($"      From email: {ActiveMailItem.Sender.Address}");
            //    Debug.WriteLine($"      Conversation ID: {ActiveMailItem.ConversationID}");
            //    Debug.WriteLine($"");
            //}
        }


        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

I have spent a long time researching this, but clearly I am missing something. I feel that I need to wait for the active inspector / current item to change before attempting to get the Active Inspector. I had a look online and found a few links inclusing this:

https://social.msdn.microsoft.com/Forums/en-US/a17733ec-4420-4c7a-aa6a-23a9c9ee68e1/how-to-get-the-mail-item-opened-window?forum=outlookdev

But this like the others is not quite what I'm looking for because I am not dealing with a new inspector. I'm dealing with one already opened and simply working between 2 or more open inspectors.

The function "Mail_Debugger" seems to work fine - when I doubleclick an email in outlook, when it opens, I get the information for the correct email that I just opened.

In closing, my goal is to be able to open multiple emails by doubleclicking in outlook to open then in their own window and then click those email windows to get the active inspector which should be able to provide information about the email in the window.

Any idea what I am doing wrong? Thankyou

question from:https://stackoverflow.com/questions/65649177/outlook-vsto-activeinspector-returns-the-incorrect-email-windows-information

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your NewInspectorCreated event will never fire - you are setting up the event handler on an implicit variable created by the compiler - as soon as GC releases it, the event will no longer fire. You must keep the variable raising events alive.

    public partial class ThisAddIn
    {
        //Outlook.Inspectors OurInspectors;
        Outlook.Inspector ActiveInspector;
        Outlook.MailItem ActiveMailItem;
        Outlook.Inspectors _inspectors;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Debug.WriteLine("LOADING CODE");
            _inspectors = Application.Inspectors;
            _inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Mail_Debugger);
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...