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


i'm building outlook add-in, , i'd react text selection changes in e-mail body.

to this, attach windowselectionchange event word object inside mail item's inspector.

if (inspector.iswordmail())   {       worddoc = inspector.wordeditor word.document;       worddoc.application.windowselectionchange += new microsoft.office.interop.word.applicationevents4_windowselectionchangeeventhandler(text_selected);   } 

this works fine bit, event stops firing on selections, seemingly @ random. i've been @ day, , having trouble solving issue.

after looking around, read worddoc object being garbage collected, ensured have set class variable.

anyway, maybe without entire project it's difficult pinpoint exact cause of problem... question more around, how go debugging this? if event isn't fired, there nothing me breakpoint. how monitor status of event listeners if they're not properties in worddoc's application?

here's of code class in question, bit removed. apologize mess - i'm learning go 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 outlook.mailitem);                     if (inspector.iswordmail())                     {                         worddoc = inspector.wordeditor 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 main addin class, in case 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 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 event. if have code              //    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         {                         {                 return inspectorwrappersvalue;             }         }          #region vsto generated code          /// <summary>         /// required method designer support - not modify         /// contents of method code editor.         /// </summary>         private void internalstartup()         {             this.startup += new system.eventhandler(thisaddin_startup);             this.shutdown += new system.eventhandler(thisaddin_shutdown);         }          #endregion     } 

it not worddoc variable raises event , being garbage collected. application object returned worddoc.application:

private word.application wordapp; ... wordapp = worddoc.application; wordapp.windowselectionchange += new microsoft.office.interop.word.applicationevents4_windowselectionchangeeventhandler(text_selected); 

Comments