View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
royend[_2_] royend[_2_] is offline
external usenet poster
 
Posts: 4
Default How do I reference a Worksheet from a Form (Add-in for Excel 2

Hi again.
I am still struggling with this, and I have decided to take another approach.

Since I am familiar with .NET-coding for web, I tried defining my Form as a
user control with a property for a Worksheet:
public partial class Form1 : Form
{
private Excel.Worksheet _excelWorksheet = null;
public Excel.Worksheet excelWorksheet { get { return
_excelWorksheet; } set { _excelWorksheet = value; } }
....

I also added a new constructor for my Form1 class which recievew my
Worksheet as an input:

public Form1(Excel.Worksheet ws)
{
InitializeComponent();
excelWorksheet = ws;
}

This excelWorksheet should then be available for my Form1 class at a later
time.

From my Add-in class I then instantiates the Form1 class with a worksheet
like this:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Excel.Worksheet activeWorksheet =
((Excel.Worksheet)Application.ActiveSheet);
Form1 form = new Form1(activeWorksheet);
form.Show();
}

However when I debug I see that the activeWorksheet is null.
What am I doing wrong?

Looking forward to any advice, hint and help.

"Jacob Skaria" wrote:

Hi

You need to add a reference to Microsoft Excel 11.0 Object Library.
Checkout this for C# code (http://www.codeproject.com/KB/office/package.aspx)

If this post helps click Yes
--------------
Jacob Skaria