I created the Excel Addin using the VSTO template (VS2010, Excel2007). In Solution Explorer, I have a group called Excel, and below it is the ExcelAddIn.cs file. This has access to the active worksheet using code like
public partial class MyAddIn { Excel.Worksheet activeWorksheet = (Excel.Worksheet)Application.Activesheet; Excel.Range firstRow = activeWorksheet.get_Range("A1",missing); }
etc .. This code works fine, i.e. I can get an Excel model.
Instead of putting all my processing code into this file of one class, I would like to work with the data of an Excel worksheet in another class file. I created this file, but I canโt use any code in it, for example, above. I cannot access the Excel model from this file. I duplicated "using Microsoft.Office.Tools.Excel links", but inserting a line, for example:
Excel.Worksheet activeWorksheet = (Excel.Worksheet)Application.Activesheet;
gives me the message "Name" of the application "does not exist in the current context."
Any ideas on what links / changes I need to make in order to get the Excel model from this separate class file?
by the way. The file that works references the Excel.Application object for this first line of code, the second separate file that does not work references the Microsoft.Office.Interop.Excel object.
Thanks Pete
==== FOUND ANSWER ==== The way to access your sheets from other classes that you add is to simply access
Globals.ThisAddIn.Application.ActiveSheet;
eg:
Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddin.Application.ActiveSheet;
where "ThisAddIn" is the name of the class created by your wizard (you may have renamed it).
So, use Globals to access Excel objects outside of ThisAddin code.