none
Word doesn't quit if you call Document.ActiveWindow in the DocumentOpen event RRS feed

  • Question

  • Hello,

    CODE. Consider a Word add-in having this code:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.DocumentOpen += Application_DocumentOpen;
    }
    
    private void Application_DocumentOpen(Word.Document Doc)
    {
        object activeWindow = null;
        try
        {
            activeWindow = Doc.GetType().InvokeMember("ActiveWindow", System.Reflection.BindingFlags.GetProperty, null, Doc, null);
        }
        catch (Exception)
        {
            // skip the exception
        }
        string message = "(not set)";
        if (activeWindow != null)
        {
            message = "Caption = " + (activeWindow as Word.Window).Caption;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(activeWindow);
        }
        else
        {
            message = "no active window";
        }
        System.Diagnostics.Debug.WriteLine("!!! " + message);
    }
    
    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        this.Application.DocumentOpen -= Application_DocumentOpen;
    }

    SCENARIO. Build the add-in project, start Word 2013 or 2016 (I tested this add-in on two machines with all updates installed), create a new document, switch to the Insert tab, in the Text group click the Object button (don't expand the list), in the Object dialog switch to the Create from File tab, specify a Word document (.docx or .doc) to be inserted and click OK; at this moment the code reports the caption of the active window. After inserting the document, close Word, and choose Don't Save. After the Word UI closes, find the WINWORD.EXE process still running.

    I'm ready to provide extra info.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Thursday, August 11, 2016 12:50 PM

All replies

  • Hi Andrei,

    Thanks for your post, I made a test and I could reproduce your issue. I assume your issue is related with “Word.Document Doc”. It seems there are references to it after you close this document, but I failed to mark it work.

    For a workaround, I would suggest you create your own Ribbon, and achieve insert the content from other documents.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, August 12, 2016 5:09 AM
  • Hello Edward,

    I know several workarounds for this issue. The whole point of my report is: one of the ways of inserting a document doesn't work correctly. Releasing the Doc variable doesn't help. Please report this issue to the Word team.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Friday, August 12, 2016 8:13 AM
  • Hi Andrei,

    It is sad there is no internal channel for us to report to Word Team. For this issue, I suggest you submit a feedback in the link below:

    https://word.uservoice.com/

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Saturday, August 13, 2016 2:37 AM