Access an Excel worksheet in a C # class file using VSTO

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.

+4
source share
2 answers

Your own answer is here:

The way to access your sheets from other classes 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.

+6
source

This may be due to the fact that the application exists only when the Excel executable file is running? Keep in mind that Addins are specifically designed for this and may have a โ€œspecial sauceโ€ in the background that allows it to interact with Office classes.

I donโ€™t think you can call Application.Activesheet from another class, because the class you are calling with is completely unaware of the existence of the application.

I understand that I may be too much, I hope that some of them made sense :)

In short, no, I do not believe that you can call Application.Activesheet like this, if possible you passed the Application object through the constructor in your instanciation of your class.

t

 MyClass c = new MyClass(this.Application); 

...

 public class MyClass { public MyClass(Application app) { // And from here manipulate the Application object (just be sure you've added the reference to the namespace that the Application object uses) } } 

I am not sure if this will work, although at present I have not booted my virtual machine :)

Good luck

0
source

All Articles