none
Word 2007 AddIn_Startup event interrupted by DocumentOpen event RRS feed

  • Question

  • Hi,

    We have a Word Addin that's working perfect when Word is called from Outlook.

    Recently we've developed a Print mechanism from our WPF Application which launches Word in order to print a XPS file.

    For some reason the Addin's code, when Word is called from the WPF Application is misbehaving with regards the AddIn_Startup and DocumentOpen events.
    It seems as if the DocumentOpen event is fired during the execution of the code we have in the AddIn_Startup event.

    I'd like to ask if there is any way to keep the code in AddIn_Startup to run until it ends without having the DocumentOpen event interrupting.
    We've printed log messages and we actually see that the code in AddIn_Startup is interrupted in favor of the code in DocumentOpen and then returns to the code of AddIn_Startup - Is this possible???

    Many thanks

    Edi

    Wednesday, October 16, 2013 8:09 PM

Answers

  • Hi Edi

    Yes, I believe this is possible. Historically, Word runs in "sync" rather than "async" mode. But in recent years more and more things execute in parallel to improve start up and processing times for the user. So under some circumstances DocumentOpen for the default blank file can trigger before add-ins complete loading. (and it's also important to realize that multiple add-ins might be working at the same time, which can also affect things).

    Would it work for you to declare a class-level member in the ThisAddin class with a default value? Have DocumentOpen check and, if the default value is still assigned, go into a loop or end. Have ThisAddin_Startup assign a different value to the class member that would allow DocumentOpen to execute. And you might also need to put the main part of that code in a separate method that can be called from DocumentOpen as well as from ThisAddin_Startup if you find working with a loop in DocumentOpen doesn't work correctly. In that case you might need an additional class-level member to track with DocumentOpen ran or had to exit without running...


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 17, 2013 4:58 PM
    Moderator

All replies

  • Hi,

    Thank you for posting in the MSDN Forum.

    According to your description, the DocumentOpen event interrupts the AddIn_Startup. It means you encounter an exception when loading the Add-in.

    I tried to reproduce the issue but failed. Below is the steps:

    Create a Word 2007 add-in and add the code below to ThisAddin.cs:

    string logFilePath = @"C:\Users\Desktop\log.txt";
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.DocumentOpen += Application_DocumentOpen;
                File.AppendAllText(logFilePath, "\r\n" + DateTime.Now.ToString() + " ThisAddIn_Startup...");
    
                System.Threading.Thread.Sleep(5000);
    
                File.AppendAllText(logFilePath, "\r\n" + DateTime.Now.ToString() + " ThisAddIn_Startup end...");
            }
    
            void Application_DocumentOpen(Word.Document Doc)
            {
                File.AppendAllText(logFilePath, "\r\n" + DateTime.Now.ToString() + " Application_DocumentOpen...");
            }

    Then I create a console application to open a Word document like code below:

    static void Main(string[] args)
            {
                OpenWordDoc(@"C:\Users\Desktop\New Microsoft Office Word Document.docx");
            }
    
            private static void OpenWordDoc(string filePath)
            {
                Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Documents.Open(filePath);
                oWord.Quit();
            }
    

    Every time the document event would execute after the function ThisAddIn_Startup finished. Did I miss anything?

    Would you mind providing us some more detail steps and code to help us to reproduce this issue.

    BTW, I suggest you try to register the DocumentOpen event at the end of ThisAddIn_Startup.

    Best regards

    Fei


    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.


    Thursday, October 17, 2013 1:10 PM
    Moderator
  • Hi Edi

    Yes, I believe this is possible. Historically, Word runs in "sync" rather than "async" mode. But in recent years more and more things execute in parallel to improve start up and processing times for the user. So under some circumstances DocumentOpen for the default blank file can trigger before add-ins complete loading. (and it's also important to realize that multiple add-ins might be working at the same time, which can also affect things).

    Would it work for you to declare a class-level member in the ThisAddin class with a default value? Have DocumentOpen check and, if the default value is still assigned, go into a loop or end. Have ThisAddin_Startup assign a different value to the class member that would allow DocumentOpen to execute. And you might also need to put the main part of that code in a separate method that can be called from DocumentOpen as well as from ThisAddin_Startup if you find working with a loop in DocumentOpen doesn't work correctly. In that case you might need an additional class-level member to track with DocumentOpen ran or had to exit without running...


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 17, 2013 4:58 PM
    Moderator