Skip to content

Instantly share code, notes, and snippets.

@crazyfool2100
Created July 13, 2011 09:18
Show Gist options
  • Save crazyfool2100/1079973 to your computer and use it in GitHub Desktop.
Save crazyfool2100/1079973 to your computer and use it in GitHub Desktop.
<DnaLibrary RuntimeVersion="v4.0" Language="C#">
<Reference Name="System.Windows.Forms" />
<Reference Name="Microsoft.Office.Interop.Excel" />
<![CDATA[
using System;
using System.Reflection;
using System.Windows.Forms;
using ExcelDna.Integration;
using ExcelDna.Integration.CustomUI;
using ExcelDna.Integration.Extensibility;
using Microsoft.Office.Interop.Excel;
public class AddIn : IExcelAddIn
{
public AddIn()
{
}
public void AutoOpen()
{
Microsoft.Office.Interop.Excel.Application app = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application;
app.SheetActivate += new AppEvents_SheetActivateEventHandler(EventHandler_SheetActivate);
app.WorkbookActivate += new AppEvents_WorkbookActivateEventHandler(EventHandler_WorkbookActivate);
app.WorkbookBeforeClose += new AppEvents_WorkbookBeforeCloseEventHandler(EventHandler_WorkbookBeforeClose);
app.SheetChange += new AppEvents_SheetChangeEventHandler(EventHandler_SheetChange);
app.WorkbookOpen += new AppEvents_WorkbookOpenEventHandler(EventHandler_WorkbookOpen);
app.SheetSelectionChange += new AppEvents_SheetSelectionChangeEventHandler(EventHandler_SheetSelectionChange);
}
public void AutoClose()
{
}
private void EventHandler_WorkbookActivate(object obj)
{
Workbook oBook = (Workbook)obj; // Cast object to Workbook
MessageBox.Show("Workbook Activate: " + oBook.Name);
}
private void EventHandler_SheetActivate(object obj)
{
Worksheet oSheet = (Worksheet)obj; // Cast object to Worksheet
MessageBox.Show("Sheet Activate: " + oSheet.Name);
}
private void EventHandler_WorkbookBeforeClose(Workbook oBook, ref bool Cancel)
{
MessageBox.Show("Workbook Before Close: " + oBook.Name);
}
private void EventHandler_SheetChange(object obj, Range Target)
{
Worksheet oSheet = (Worksheet)obj;
MessageBox.Show("Sheet Change: " + oSheet.Name);
}
private void EventHandler_WorkbookOpen(Workbook oBook) // Doesn't fire when you create a new workbook
{
MessageBox.Show("Workbook Open: " + oBook.Name);
}
private void EventHandler_SheetSelectionChange(object obj, Range Target)
{
Worksheet oSheet = (Worksheet)obj;
MessageBox.Show("Sheet Selection Change: " + oSheet.Name);
}
}
]]>
</DnaLibrary>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment