Hi,
I have a VSTO addin which has a custom pane and a timer which updates a cell in the spreadsheet with a random number. This works fine until you start clicking around the sheet, and then I get an exception.
Have left in some alternative ways of accessing the sheet commented out, same result in all of them. Same result using Dispatcher (Invoke or BeginInvoke) or not. Code extract:-
private void timer1_Tick(object sender, EventArgs e)
{
double myNewExposure = getSHTExposure();
txtTotalExposure.Text = myNewExposure.ToString();
if (myNewExposure <= -1)
{
myHedges = rnd.Next(1, 20);
if (saveHedge(myHedges))
{
}
txtLastHedge.Text = myHedges.ToString();
txtLastUpdated.Text = DateTime.Now.ToString("h:mm:ss tt");
lstAudit.Items.Add("Hedge of " + txtLastHedge.Text + " placed at " + txtLastUpdated.Text);
//Microsoft.Office.Tools.Excel.Worksheet nativeWorksheet = ((Microsoft.Office.Tools.Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet);
Excel.Worksheet nativeWorksheet = Globals.ThisAddIn.Application.ActiveSheet;
if (nativeWorksheet != null)
{
Microsoft.Office.Tools.Excel.Worksheet vstoSheet = Globals.Factory.GetVstoObject(nativeWorksheet);
try{
System.Action action = () =>
{
//vstoSheet.InnerObject.get_Range("A2", Type.Missing).Value2 = txtLastHedge.Text;
//nativeWorksheet.InnerObject.get_Range("A2", Type.Missing).Value2 = txtLastHedge.Text;
nativeWorksheet.get_Range("A2", Type.Missing).Value2 = txtLastHedge.Text;
};
Globals.ThisAddIn.Dispatcher.Invoke(action);
//Globals.ThisAddIn.Dispatcher.BeginInvoke(action);
}
catch (Exception ex)
{
MessageBox.Show ("Exception : " + ex.Message);
}
}
}
}
Exception I get is:- A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
This is my first VSTO project so a bit mystified, and I am by no means a C# expert, so any advice gratefully accepted.