none
EventHandler on docB is lost when docA is closed / EventHandler does not register on reopen of docB RRS feed

  • Question

  • Hi there

    I am using Visual Studio 2012 for development of a Word 2010 application Add-In.

    My application Add-In is registering an EventHandler when a document is opened.

    This does not always work as I would expect.

    Example 1, EventHandler on documentB.docx is lost when documentA.docx is closed

    Open documentA.docx -- EventHandler is registered and event fires happily away on documentA.dox.

    Open documentB.docx -- EventHandler is registered and event fires happily away on documentB.dox.

    Close documentA.docx -- Event does not fire anymore on documentB.docx that still is open.

    Example 2, event does not fire on documents that are reopened in the same Word instance

    Open documentA.docx -- EventHandler is registered and event fires happily away.

    Open documentB.docx -- EventHandler is registered and event fires happily away.

    Close documentB.dox and reopen it -- EventHandler is registered (at least the code runs through without throwing an exception), but event does not fire on documentB.docx. It still fires on documentA.docx though.

    This is what I call on DocumentOpen(Microsoft.Office.Interop.Word.Document doc):

    Microsoft.Office.Tools.Word.Document vstoDoc = Globals.Factory.GetVstoObject(doc);
    vstoDoc.BeforeDoubleClick += new Microsoft.Office.Tools.Word.ClickEventHandler(ThisDocument_BeforeDoubleClick);

    Does somebody know what I am doing wrong and how I could do better?

    Thanks in advance, kind regards

    Nora


    Sunday, June 5, 2016 4:25 PM

Answers

  • >>>Did you already find a workaround or even a solution?

    You could modify your codes like below:
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Application.DocumentOpen += new ApplicationEvents4_DocumentOpenEventHandler(ThisAddIn_DocumentOpen);
        Application.DocumentBeforeClose += new ApplicationEvents4_DocumentBeforeCloseEventHandler(ThisAddIn_DocumentBeforeClose);
    }
    
    private void ThisAddIn_DocumentOpen(Microsoft.Office.Interop.Word.Document doc)
    {
        (Globals.Factory.GetVstoObject(Application.ActiveDocument)).BeforeDoubleClick += ThisDocument_BeforeDoubleClick;
    }
    
    private void ThisAddIn_DocumentBeforeClose(Word.Document Doc, ref bool Cancel)
    {
        (Globals.Factory.GetVstoObject(Application.ActiveDocument)).BeforeDoubleClick -= ThisDocument_BeforeDoubleClick;
    }
    
    private void ThisDocument_BeforeDoubleClick(object sender, Microsoft.Office.Tools.Word.ClickEventArgs e)
    {
        MessageBox.Show("Before doubleclick");
    }

    • Marked as answer by nora.sailer Friday, July 8, 2016 8:00 AM
    Thursday, July 7, 2016 8:37 AM

All replies

  • Hi nora.sailer,

    According to your description, when you programmatically create or open a document at run time by using the Word, the item is not a host item. Instead, the new object is a native Office object. For example, if you use the M:Microsoft.Office.Interop.Word.Documents.Add(System.Object@,System.Object@,System.Object@,System.Object@) method to create a new Word document at run time, it will be a native Microsoft.Office.Interop.Word.Document object rather than a Microsoft.Office.Tools.Word.Document host item.

    If you want to generate a host item for a Word document, you could refer to below code:
    if (Globals.ThisAddIn.Application.Documents.Count > 0)
    {
        Microsoft.Office.Interop.Word.Document nativeDocument =
            Globals.ThisAddIn.Application.ActiveDocument;
        Microsoft.Office.Tools.Word.Document vstoDocument =
            Globals.Factory.GetVstoObject(nativeDocument);
    }

    So I suggest that you could modify your code like below:
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.BeforeDoubleClick += new Microsoft.Office.Tools.Word.ClickEventHandler(ThisDocument_BeforeDoubleClick);
    For more information, click here to refer about Programmatic Limitations of Host Items and Host Controls and here to refer about Document.BeforeDoubleClick Event

    Monday, June 6, 2016 1:44 AM
  • Hi David

    Thanks for you reply. Unfortunately, the modification you suggest does not change the behaviour I describe above.

    I created a new Application project for easier reproduction of the problem. This is the only code, in ThisAddIn.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools.Word;
    using Microsoft.Office.Interop.Word;
    using System.Windows.Forms;
    
    namespace TestBeforeDoubleClick
    {
        public partial class ThisAddIn
        {
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
              Application.DocumentOpen += new ApplicationEvents4_DocumentOpenEventHandler(ThisAddIn_DocumentOpen);
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            private void ThisAddIn_DocumentOpen(Microsoft.Office.Interop.Word.Document doc)
            {
              Microsoft.Office.Tools.Word.Document vstoDoc = Globals.Factory.GetVstoObject(Application.ActiveDocument);
              vstoDoc.BeforeDoubleClick += new Microsoft.Office.Tools.Word.ClickEventHandler(ThisDocument_BeforeDoubleClick);
            }
    
            private void ThisDocument_BeforeDoubleClick(object sender, Microsoft.Office.Tools.Word.ClickEventArgs e)
            {
              MessageBox.Show("Before doubleclick");
            }
    
            #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
        }
    }
    

    Any other suggestions?

    Actually, for the time being, I only need the doubleclick on ContentControls. That's why I first tried to react to the OnEnter event of the ContentControl. But this event is also triggered by code, and I only want to react to it when it is triggered by the user. So if anybody knows how to make this difference, I would be happy for the moment.

    But it still is strange that the BeforeDoubleClick event on the document gets lost...

    Thanks and kind regards

    Nora


    • Edited by nora.sailer Monday, June 6, 2016 10:50 AM Example of ThisAddIn.cs added, for easier reproduction
    Monday, June 6, 2016 10:34 AM
  • Hi nora.sailer,

    Thanks very much for your detail steps to help us reproduce this issue. I’m able to reproduce this issue now. Unfortunately, I did not see any workaround to overcome this. I will still keep troubleshooting this issue but it will take some time. 

    Thanks for your understanding.
    Wednesday, June 8, 2016 1:48 AM
  • Hi David

    Did you already find a workaround or even a solution?

    If not: For the time being, I only need the doubleclick on ContentControls. That's why I first tried to react to the OnEnter event of the ContentControl. But this event is also triggered by CODE, and I only want to react to it when it is triggered by the USER. So if you or any other reader knows how to make this difference, I would be very glad to get an answer to that one.

    Kind regards

    Nora

    Wednesday, July 6, 2016 5:42 PM
  • >>>Did you already find a workaround or even a solution?

    You could modify your codes like below:
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Application.DocumentOpen += new ApplicationEvents4_DocumentOpenEventHandler(ThisAddIn_DocumentOpen);
        Application.DocumentBeforeClose += new ApplicationEvents4_DocumentBeforeCloseEventHandler(ThisAddIn_DocumentBeforeClose);
    }
    
    private void ThisAddIn_DocumentOpen(Microsoft.Office.Interop.Word.Document doc)
    {
        (Globals.Factory.GetVstoObject(Application.ActiveDocument)).BeforeDoubleClick += ThisDocument_BeforeDoubleClick;
    }
    
    private void ThisAddIn_DocumentBeforeClose(Word.Document Doc, ref bool Cancel)
    {
        (Globals.Factory.GetVstoObject(Application.ActiveDocument)).BeforeDoubleClick -= ThisDocument_BeforeDoubleClick;
    }
    
    private void ThisDocument_BeforeDoubleClick(object sender, Microsoft.Office.Tools.Word.ClickEventArgs e)
    {
        MessageBox.Show("Before doubleclick");
    }

    • Marked as answer by nora.sailer Friday, July 8, 2016 8:00 AM
    Thursday, July 7, 2016 8:37 AM
  • Hey, this is great, thanks a lot!

    Happy greetings

    Nora

    Friday, July 8, 2016 8:00 AM