Jack
Jack

Reputation: 16724

DocumentOpen Word event is not working

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?

Upvotes: 0

Views: 1005

Answers (1)

Ken White
Ken White

Reputation: 125661

Because you're attaching the DocumentOpen event after the document has already been opened, so there's no reason for it to be called.

Quit works because, well, you haven't quit the WordApplication yet when it's attached.

Attach both events before you call DocumentOpen to open the document.

Upvotes: 3

Related Questions