How do I program a COM Event in Visual C#?

  1. Create a new project with a reference to the HotDocs Type Library in Visual Studio:
  1. Create a new Visual C# Console Application project.

  2. Right click on References from the Solution Explorer and Add Reference.  The Add Reference dialog box appears.

  3. From the COM type libraries, select the HotDocs 11 Type Library, then click OK.

  4. Add a using HotDocs; statement to the project.

  5. Add a using System.Runtime.InteropServices; statement so you can release the COM object when finished with it.

  1. Create a new Application object:
Application app = new Application();
  1. Create an event handler that will be called when the event is fired:
  1. Directly below the new Application object type app.OnAssemblyStartEvent += and then tab twice to generate the method stub for displaying a message when the event fires.  This will add the following to the static void Main method.

app.OnAssemblyStartEvent += app_OnAssemblyStartEvent;
  1. Comment out throw new NotImplementedException();

  2. Directly below that, add the following code to the newly created event:

Console.WriteLine("OnAssemblyStart");
  1. Add code to start the assembly so you can see your event:
string tplPath, tplTitle, tplDesc;
app.SelectTemplate("", true, out tplPath, out tplTitle, out tplDesc);
 
if (tplPath != "")
    app.Assemblies.AddToQueue(tplPath); 
 
Console.ReadLine();
  1. Release the COM object:
Marshal.ReleaseComObject(app);
  1. Run the application.  
  2. Result:

The HotDocs SelectTemplate interface appears, allowing you to select a template from the library.  When you select a template, it is added to the assembly queue.  Then when the assembly starts, the app.OnAssemblyStart event fires and the console displays the message telling you that it fired.