简体   繁体   中英

C# VSTO Outlook Add-in - Debugging Text Selection Event?

I'm building an Outlook Add-in, and I'd like to react to text selection changes in the e-mail body.

To to this, I attach a WindowSelectionChange event to the Word object inside the mail item's inspector.

if (inspector.IsWordMail())
  {
      wordDoc = inspector.WordEditor as Word.Document;
      wordDoc.Application.WindowSelectionChange += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventHandler(text_selected);
  }

This works fine for a bit, but then the event stops firing on selections, seemingly at random. I've been at this all day, and am having trouble solving the issue.

After looking around, I read that it could be that the wordDoc object is being garbage collected, so I ensured that I have it set as a class variable.

Anyway, maybe without the entire project it's too difficult to pinpoint the exact cause of the problem... My question is more around, how would I go about debugging this? If the event isn't fired, there is nothing for me to breakpoint. How do I monitor the status of my event listeners if they're not properties in the wordDoc's application?

Here's most of the code from the class in question, with a bit removed. I apologize for the mess - I'm learning as I go with this...

 public class InspectorWrapper
        {
            private Outlook.Inspector inspector;
            private CustomTaskPane taskPane;
            private Dictionary<string, List<ScanResult>> mailItemEntities;

            private bool activated, loaded;
            private Word.Document wordDoc;

            public InspectorWrapper(Outlook.Inspector Inspector)
            {
                inspector = Inspector;

                loaded = false;
                activated = false;

                ((Outlook.InspectorEvents_Event)inspector).Close +=
                    new Outlook.InspectorEvents_CloseEventHandler(InspectorWrapper_Close);

                taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(
                    new TaskPaneControl(), "Addin", inspector);
                taskPane.VisibleChanged += new EventHandler(TaskPane_VisibleChanged);

                mailItemEntities = new Dictionary<string, List<ScanResult>>();

                ((Outlook.InspectorEvents_10_Event)inspector).Activate += new Outlook.InspectorEvents_10_ActivateEventHandler(ThisAddIn_Activate);
            }

            void ThisAddIn_Activate()
            {
                activated = true;
            }

            void text_selected(Word.Selection selected_text)
            {
                if (activated && loaded)
                {
                    ((TaskPaneControl)this.CustomTaskPane.Control).text_selected(selected_text.Text, selected_text.End - selected_text.Start);

                }
            }

            void TaskPane_VisibleChanged(object sender, EventArgs e)
            {
                Globals.Ribbons[inspector].Ribbon1.toggleButton1.Checked = taskPane.Visible;
                if (!loaded) {
                    process_email(this.inspector.CurrentItem as Outlook.MailItem);
                    if (inspector.IsWordMail())
                    {
                        wordDoc = inspector.WordEditor as Word.Document;
                        wordDoc.Application.WindowSelectionChange += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventHandler(text_selected);
                    }
                    loaded = true;
                }
            }

            void InspectorWrapper_Close()
            {
                if (taskPane != null)
                {
                    Globals.ThisAddIn.CustomTaskPanes.Remove(taskPane);
                }

                activated = false;
                loaded = false;
                taskPane = null;
                Globals.ThisAddIn.InspectorWrappers.Remove(inspector);
                ((Outlook.InspectorEvents_Event)inspector).Close -=
                    new Outlook.InspectorEvents_CloseEventHandler(InspectorWrapper_Close);
                ((Outlook.InspectorEvents_10_Event)inspector).Activate -=
                    new Outlook.InspectorEvents_10_ActivateEventHandler(ThisAddIn_Activate);
                inspector = null;
            }
        }

Here is my main Addin class, in case it matters:

    public partial class ThisAddIn
    {
        Outlook.Inspectors inspectors;
        Outlook.MailItem mailItem;
        private string last_id = "";
        private Dictionary<Outlook.Inspector, InspectorWrapper> inspectorWrappersValue =
             new Dictionary<Outlook.Inspector, InspectorWrapper>();
        private Outlook.Inspector ins;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            inspectors = this.Application.Inspectors;

            inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

        }

        void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {

            ins = Inspector;
            Outlook.MailItem mailItem = ins.CurrentItem as Outlook.MailItem;
            if (mailItem != null)
            {
                inspectorWrappersValue.Add(ins, new InspectorWrapper(ins));
            }
        }

        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 http://go.microsoft.com/fwlink/?LinkId=506785
            inspectors.NewInspector -=
                new Outlook.InspectorsEvents_NewInspectorEventHandler(
                Inspectors_NewInspector);
            inspectors = null;
            inspectorWrappersValue = null;
        }

        public Dictionary<Outlook.Inspector, InspectorWrapper> InspectorWrappers
        {
            get
            {
                return inspectorWrappersValue;
            }
        }

        #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
    }

It is not the wordDoc variable that raises the event and is being Garbage Collected. It is the Application object returned from wordDoc.Application:

private Word.Application wordApp;
...
wordApp = wordDoc.Application;
wordApp.WindowSelectionChange += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventHandler(text_selected);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM