How do I attach event handlers to a late-bound object (C#)?

E

ERitzie

It was suggested in another forum that I post this question here so here is
goes.

I've created a control to embed a MSWord document based on several projects
here. The embeding it's self works great but I am having trouble figuring out
how to bind event handlers (see code snip below). Early binding requires that
you establish a project reference to the assembly you wish to use. I prefer
not to do that since I do not have control over what version of the assembly
will be present on the various users machines.

using System.Reflection;
using System.Runtime.InteropServices;

namespace WordViewer{

public partial class WordViewer : UserControl{

public WordViewer(){
InitializeComponent();

// Create interop object
Type wordType = Type.GetTypeFromProgID("Word.Application");
object wordApplication = Activator.CreateInstance(wordType);

// Set interop object property value
Type type = wordAplication.GetType();
type.InvokeMember("CommandBars", BindingFlags.SetProperty, null,
wordApplication, new object[]{false});

// Set interop object event handlers
// This is how it is done with early binding.
// wordApplication.DocumentBeforeClose += new
ApplicationEvents4_DocumentBeforeCloseEventHandler(OnClose);
// How do I do this via late-binding?

}
}
}

In this particular instance I am trying to embed an MS Word document into my
app, but this functionaly can and should apply to any interop references. I
have no problems getting and setting properties of the interop object or even
calling methods within the object, but I can not find any information on
binding to events contained within the interop object. I am not writing this
code within Word or even using VSTO.

Thank you,
Eric Ritzie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top