Reputation: 31847
I'm writing code to access the MS Word automation COM interface using dynamic types in C# 4.0. It works great and it is very easy to use.
What I don't know is how to subscribe events. I would like to subscribe to the Application::Quit event.
This is the code I have written:
static class Program
{
[STAThread]
static void Main()
{
Type wordType = Type.GetTypeFromProgID("Word.Application");
dynamic word = Activator.CreateInstance(wordType);
var myDoc = word.Documents.Open(@"C:\example.docx");
word.Visible = true;
//how can I subscribe to the word.Quit event??
}
Upvotes: 0
Views: 441
Reputation: 2541
This should work:
((Microsoft.Office.Interop.Word.ApplicationEvents4_Event)word).Quit += OnQuit;
...and then...
private void OnQuit()
{
MessageBox.Show("Quit");
}
Upvotes: 1