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

  • Question

  • 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 below for an example.

    Word.DocumentBeforeClose += new ApplicationEvents4_DocumentBeforeCloseEventHandler(OnClose);

    The above line works with doing early binding, but due to the variations in version of Office being used in the company, I need to do late binding. Anyone got any ideas?

    Thank you,
    Eric Ritzie
    Friday, December 11, 2009 6:55 PM

Answers

  • It is possible, but not exactly straight forward. You use the IConnectionPoint and IConnectionPointContainer COM interfaces (from the System.Runtime.InteropServices.ComTypes namespace) to hook it up. But there's some magic implemented in interop assemblies that make them so much easier to use when it comes to event handling.

    If you have the option to use the VS 2010 tools, you can still reference an interop assembly and enable the type embedding feature to not have a runtime dependency on it.

    Mattias, C# MVP
    Tuesday, December 15, 2009 9:52 AM
    Moderator

All replies

  • Hello Eric,
    What do you mean by early binding and late binding? (Do you mean VB late binding?)
    Why should it magically help with earlier versions of Office?

    In general this looks like Office interop question and not CLR question to me ... (see this list of MS Office resources: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/063a23a6-1595-4c83-a25f-6c94658c4649)
    -Karel
    Monday, December 14, 2009 5:51 AM
    Moderator
  • Hi Karel,
        What I mean by late binding is that I am not referencing a specific assembly in the project references but rather using the System.Runtime.Interop to attach to the needed assembly  (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});
        }
    }
    }

    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. If you feel that my question is still specific to Office, then I will gladly post my question over in those boards, but I am looking at the question in a more generic cense even though I happen to be using Word as the example here.

    Thank you,
    Eric Ritzie

    Monday, December 14, 2009 6:23 PM
  • It is possible, but not exactly straight forward. You use the IConnectionPoint and IConnectionPointContainer COM interfaces (from the System.Runtime.InteropServices.ComTypes namespace) to hook it up. But there's some magic implemented in interop assemblies that make them so much easier to use when it comes to event handling.

    If you have the option to use the VS 2010 tools, you can still reference an interop assembly and enable the type embedding feature to not have a runtime dependency on it.

    Mattias, C# MVP
    Tuesday, December 15, 2009 9:52 AM
    Moderator
  • Thanks Mattias. I'll look into the IConnectionPoint interfaces. Once my MSDN is ready I will download VS2010 and check out what your refering to. My issue is that there are about 4 or 5 different flavors of Office and other tools throughout the company. Right now I'm working on an app for transcriptionist so that all the tools they need is contained within a single gui. This is just a prototype/proof of concept right now so that I can provide options to the CEO.

    Thank you,
    Eric Ritzie
    Tuesday, December 15, 2009 4:18 PM