Created
July 13, 2011 09:32
-
-
Save crazyfool2100/1079996 to your computer and use it in GitHub Desktop.
Excel-DNA solution to errors that result from attaching to Excel events within a managed add-in and then attemping to edit embedded sheets within Word. Based on code provided at: http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/04/11/excel-ole-embed
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
using System; | |
using System.Windows.Forms; | |
using System.Reflection; | |
using System.Runtime.InteropServices; | |
using System.Runtime.InteropServices.ComTypes; | |
using Excel = Microsoft.Office.Interop.Excel; | |
using Office = Microsoft.Office.Core; | |
using ExcelDna.Integration; | |
using ExcelDna.Integration.Extensibility; | |
using ExcelDna.Integration.CustomUI; | |
namespace xlEvents | |
{ | |
public class Connect : ExcelDna.Integration.CustomUI.ExcelComAddIn | |
{ | |
private Excel.Application applicationObject; | |
private object addInInstance; | |
private xlEvents m_xlAppEvents; | |
public Connect() | |
{ | |
} | |
public override void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom) | |
{ | |
applicationObject = (Excel.Application)Application; | |
addInInstance = AddInInst; | |
m_xlAppEvents = new xlEvents(); | |
m_xlAppEvents.DisableEventsIfEmbedded = true; | |
m_xlAppEvents.SetupConnection(applicationObject); | |
m_xlAppEvents.xlWorkbookOpen += new xlEvents.DWorkbookOpen(xlWorkbookOpen); | |
m_xlAppEvents.xlWorkbookActivate += new xlEvents.DWorkbookActivate(xlWorkbookActivate); | |
m_xlAppEvents.xlSheetActivate += new xlEvents.DSheetActivate(xlSheetActivate); | |
m_xlAppEvents.xlWorkbookBeforeClose += new xlEvents.DWorkbookBeforeClose(xlWorkbookBeforeClose); | |
m_xlAppEvents.xlSheetChange += new xlEvents.DSheetChange(xlSheetChange); | |
m_xlAppEvents.xlSheetSelectionChange += new xlEvents.DSheetSelectionChange(xlSheetSelectionChange); | |
} | |
public override void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom) | |
{ | |
} | |
public override void OnAddInsUpdate(ref Array custom) | |
{ | |
} | |
public override void OnStartupComplete(ref Array custom) | |
{ | |
} | |
public override void OnBeginShutdown(ref Array custom) | |
{ | |
m_xlAppEvents.RemoveConnection(); | |
m_xlAppEvents = null; | |
System.Runtime.InteropServices.Marshal.ReleaseComObject(applicationObject); | |
} | |
public void xlWorkbookOpen(Excel._Workbook oBook) | |
{ | |
MessageBox.Show("Workbook Open: " + oBook.Name); | |
} | |
private void xlWorkbookActivate(Excel._Workbook oBook) | |
{ | |
MessageBox.Show("Workbook Activate: " + oBook.Name); | |
} | |
private void xlSheetActivate(object oSheet) | |
{ | |
MessageBox.Show("Sheet Activate: " + ((Excel._Worksheet)oSheet).Name); | |
} | |
private void xlWorkbookBeforeClose(Excel._Workbook oBook, ref bool Cancel) | |
{ | |
MessageBox.Show("Workbook Before Close: " + oBook.Name); | |
} | |
private void xlSheetChange(object oSheet, Excel.Range oTarget) | |
{ | |
MessageBox.Show("Sheet Change: " + ((Excel._Worksheet)oSheet).Name); | |
} | |
private void xlSheetSelectionChange(object oSheet, Excel.Range oTarget) | |
{ | |
MessageBox.Show("Sheet Selection Change: " + ((Excel._Worksheet)oSheet).Name); | |
} | |
} | |
public class AddIn : IExcelAddIn | |
{ | |
private ExcelComAddIn com_addin; | |
public AddIn() | |
{ | |
} | |
public void AutoOpen() | |
{ | |
try | |
{ | |
com_addin = new Connect(); | |
// 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) | |
{ | |
MessageBox.Show("Error loading COM AddIn: " + e.ToString()); | |
} | |
} | |
public void AutoClose() | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment