Skip to content

Instantly share code, notes, and snippets.

@govert
Created July 13, 2011 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save govert/1079888 to your computer and use it in GitHub Desktop.
Save govert/1079888 to your computer and use it in GitHub Desktop.
Excel-DNA (http://excel-dna.net) sample showing how to make a COM Add-In inside the Excel-DNA .xll. Includes reflection-based workaround for version 0.29.
<DnaLibrary RuntimeVersion="v4.0" Language="C#">
<Reference Name="System.Windows.Forms" />
<![CDATA[
using System;
using System.Reflection;
using SWF = System.Windows.Forms;
using ExcelDna.Integration;
using ExcelDna.Integration.CustomUI;
using ExcelDna.Integration.Extensibility;
public class MyCom : ExcelDna.Integration.CustomUI.ExcelComAddIn
{
public MyCom()
{
}
public override void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
{
SWF.MessageBox.Show("OnConnection");
}
public override void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
{
SWF.MessageBox.Show("OnDisconnection");
}
public override void OnAddInsUpdate(ref Array custom)
{
SWF.MessageBox.Show("OnAddInsUpdate");
}
public override void OnStartupComplete(ref Array custom)
{
SWF.MessageBox.Show("OnStartupComplete");
}
public override void OnBeginShutdown(ref Array custom)
{
SWF.MessageBox.Show("OnBeginShutDown");
}
}
public class AddIn : IExcelAddIn
{
private ExcelComAddIn com_addin;
public AddIn()
{
}
public void AutoOpen()
{
try
{
com_addin = new MyCom();
// We want to do this:
// com_addin.DnaLibrary = ExcelDna.Integration.DnaLibrary.CurrentLibrary;
// But the DnaLibrary property is marked 'internal' to ExcelDna.Integration.
// v0.29 workaround: set by Reflection
com_addin.GetType().InvokeMember("DnaLibrary",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty,
null, com_addin, new object[] {DnaLibrary.CurrentLibrary});
ExcelComAddInHelper.LoadComAddIn(com_addin);
}
catch (Exception e)
{
SWF.MessageBox.Show("Error loading COM AddIn: " + e.ToString());
}
}
public void AutoClose()
{
}
}
]]>
</DnaLibrary>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment