I have this code that open from my C# application a doc file:.
var wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Documents.Open(FileName);
wordApp.Visible = true;
wordApp.ActiveWindow.View.FullScreen = true;
var events = (Microsoft.Office.Interop.Word.ApplicationEvents4_Event) wordApp;
events.DocumentOpen += delegate { MessageBox.Show("opended!"); };
events.Quit += delegate { MessageBox.Show("closed!"); };
But the document open and I don’t get MessageBox.Show("opended!") but MessageBox.Show("closed!") works fine. How to fix this?
Because you’re attaching the
DocumentOpenevent after the document has already been opened, so there’s no reason for it to be called.Quitworks because, well, you haven’t quit theWordApplicationyet when it’s attached.Attach both events before you call
DocumentOpento open the document.