Created
July 13, 2011 07:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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