Skip to content

Instantly share code, notes, and snippets.

@govert
Created September 7, 2011 09:48
Show Gist options
  • Save govert/1200178 to your computer and use it in GitHub Desktop.
Save govert/1200178 to your computer and use it in GitHub Desktop.
Excel-DNA (http://excel-dna.net) sample showing how to load/unload other add-ins from a host add-in
<DnaLibrary Name="AddInHost" Language="C#" RuntimeVersion="v4.0">
<Reference Name="System.Windows.Forms" />
<![CDATA[
using System;
using System.Windows.Forms;
using ExcelDna.Integration;
public class MyMacros
{
public static object registerId;
[ExcelCommand(MenuName="AddinHost", MenuText="Load Addin")]
public static void LoadAddin()
{
XlCall.XlReturn result = XlCall.TryExcel(XlCall.xlfRegister, out registerId, @"C:\Work\ExcelDna\Samples\MetaAddin\AddIn.xll");
}
[ExcelCommand(MenuName="AddinHost", MenuText="Unload Addin")]
public static void UnloadAddin()
{
MessageBox.Show("About to call xlAutoRemove.");
string theName = @"C:\Work\ExcelDna\Samples\MetaAddin\AddIn.xll";
object removeId = XlCall.Excel(XlCall.xlfRegister, theName, "xlAutoRemove", "I" , ExcelMissing.Value, ExcelMissing.Value, 2);
object removeResult = XlCall.Excel(XlCall.xlfCall, removeId);
object removeUnregister = XlCall.Excel(XlCall.xlfUnregister, removeId);
object success = XlCall.Excel(XlCall.xlfUnregister, registerId);
MessageBox.Show("Result of Unregister: " + success.ToString() );
}
}
public static class MyFunctions
{
public static string AddinHostTestFunction()
{
return "Hello from the add-in host!";
}
[ExcelCommand(MenuName="AddinHost", MenuText="Say Hello!")]
public static void AddIn()
{
MessageBox.Show("Hello from the add-in host!");
}
}
]]>
</DnaLibrary>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment