none
Handling Word Events in c# .net client RRS feed

  • Question

  • Hello,

    our WPF application starts a Word instance and we want to be informed, when Word quits.

    So we wrote an event handler to receive a close event.

    The Problem is, sometimes it works and sometimes it doesn't.

    What did we do wrong ?

    To receive the proper event we coded:

    // Open Word with a given Document Path

    Microsoft.Office.Interop.Word.Document document = ap.Documents.Open(path, false, true, false);

    // WordDocumentEventHandler is our Listener

    WordDocumentEventHandler WordCloseEventHandler = new WordDocumentEventHandler();

    ap.DocumentBeforeClose += WordCloseEventHandler.DocumentBeforeCloseEvent;

    Thanks in advance, Rolf

    Wednesday, March 18, 2015 1:48 PM

Answers

  • You need to declare the source object at the global scope to keep it alive. Or the garbage collector can swipe the heap and remove the event source object. Thus, you will not get any event when document is closed.
    Wednesday, March 18, 2015 2:00 PM
  • Hi RolfBechker,

    As Eugene metioned, we need to ensuer Word.Application was not realsed by the Garbage Collection. To achieve the goal, we can declare it as gloable variable.

    I noticed that you declare it in a method, please try to declare it as a class level variable and make sure this object of this class was not realease throughout the process you expect the event works.

    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, March 26, 2015 8:13 AM
    Moderator

All replies

  • You need to declare the source object at the global scope to keep it alive. Or the garbage collector can swipe the heap and remove the event source object. Thus, you will not get any event when document is closed.
    Wednesday, March 18, 2015 2:00 PM
  • Hello Eugene,

    thanks for your very fast response.

    I did what you mentioned, with the effect, that it seemed to work better.

    But with more intensive testing, it is the same as before.

    It works fine for a number of times, but suddenly it seems, as if the event firing stops for no reason.

    Do you have any Ideas ?

    From my point of view, it looks like a bug in Office (i know, that is impossible ;)

    Wednesday, March 18, 2015 3:46 PM
  • What code do you have now?
    Wednesday, March 18, 2015 4:25 PM
  • Hi Eugene,

    my current code is:

    WordDocumentEventHandler WordCloseEventHandler = GlobalModel.createNew(info);
    ap.DocumentBeforeSave += WordCloseEventHandler.DocumentBeforeSaveEvent;

    I changed to the Save Event for several reasons, but that shouldn't affect the solution.

    The method call "GlobalModel.createNew(info)" is an add Statement to a static List of Eventhandlers, just to proof the concept of a global scope.

    Thank you in advance.

    Wednesday, March 18, 2015 4:51 PM
  • Where is the ap object declared?
    Wednesday, March 18, 2015 5:29 PM
  • The ap Object is declared inside my OpenWord - Method.

    The class itself is bound to a GUI button as a command.

    
    public class OfficeCommand : ICommand
    {
        
        public void Execute(object parameter)
            {
              .
              .
              OpenWord(info, ReadWrite);
             }
        
        private void OpenWord(DocumentInfo info, bool readwrite)
            {
                .
                .
                Word.Application ap = new Word.Application();
                .
                Microsoft.Office.Interop.Word.Document document
                             = ap.Documents.Open(path,
                                                                 ConfirmConversions:false,
                                                                 ReadOnly:!readwrite,
                                                                 AddToRecentFiles:false);
                .
                WordDocumentEventHandler WordCloseEventHandler = GlobalModel.createNew(info);
                ap.DocumentBeforeSave += WordCloseEventHandler.DocumentBeforeSaveEvent;
                ap.Visible = true;
                ap.Activate();
                .
                .
            }
    }

    Thursday, March 19, 2015 7:51 AM
  • And this is the event handler:

    public class WordDocumentEventHandler
        {

            public void DocumentBeforeSaveEvent(Word.Document doc, ref bool SaveAsUI, ref bool Cancel) {
                if (doc.Words.Count > 1)
                {
                    ...// nothing Document specific
                }
                else
                {
                    ....// nothing Document specific
                }
                doc.Save();
                Console.Out.WriteLine("Saved...");
            }
        }

    Thursday, March 19, 2015 7:55 AM
  • Hi RolfBechker,

    As Eugene metioned, we need to ensuer Word.Application was not realsed by the Garbage Collection. To achieve the goal, we can declare it as gloable variable.

    I noticed that you declare it in a method, please try to declare it as a class level variable and make sure this object of this class was not realease throughout the process you expect the event works.

    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, March 26, 2015 8:13 AM
    Moderator
  • Hello Fei,

    thank you for answering. Your suggestion was absolutely right.

    I declared the Word.Application as a class variable and since then it works perfect.

    Thank you & Eugene very much for helping me out.

    Friday, March 27, 2015 1:56 PM