PC Review


Reply
Thread Tools Rate Thread

How do I reference a Worksheet from a Form (Add-in for Excel 2003)

 
 
royend
Guest
Posts: n/a
 
      16th Mar 2009
Hi.
How can I reference the Worksheet (and Application) from a Form in an Add-in
for Excel 2003? I have tried this code:

Excel.Worksheet sheet = (Excel.Worksheet)this.Sheets["Sheet2"];
sheet.Select(Type.Missing);
Excel.Range newFirstRow = sheet.get_Range("A1", Type.Missing);
newFirstRow.Value2 = "This text was added by using code";

But I only get an error telling me that 'Form1' does not contain a
definition for 'Worksheets'.

I also tried this:

Excel.Worksheet sheet =
((Excel.Worksheet)Application.ActiveSheet);

Which led to the following error:
'System.Windows.Forms.Application' does not contain a definition for
'ActiveSheet'

My goal is to do some calculations with my Add-in, present a summary of the
calculations in the Add-in while saving all calculations in the Excel calling
the Add-in.
I am using Visual Studio 2008 for programming an Add-in for Excel 2003.
 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      16th Mar 2009
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


 
Reply With Quote
 
royend
Guest
Posts: n/a
 
      16th Mar 2009
Hi.
Thanks for your reply.
In the code of your link the programmer always needs to open an Excel-file.
Do you know if it is possible to reference the already opened
Excel-application?

A normal scenario would be that the user has opened his/hers Excel Workbook
and then selected my Add-in to do some analysis. It should not be necessary
to reopen the Excel-file, but automatically use the open workbook. But how
can I do this?

Best regards,
royend

"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
>
>

 
Reply With Quote
 
royend
Guest
Posts: n/a
 
      17th Mar 2009
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
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How do you reference an excel range in another worksheet? GG Microsoft C# .NET 0 16th Sep 2009 07:57 PM
Excel 2003 - Copy Down Function to Reference Next Worksheet Lmarsh1693 Microsoft Excel Worksheet Functions 3 18th Jul 2008 07:38 PM
Worksheet X reference MS Excel 2007 Glenda Microsoft Excel Misc 4 28th Feb 2008 11:53 PM
How do I save worksheet form data into a table in Excel 2003? =?Utf-8?B?Qm9iIFMu?= Microsoft Excel Misc 0 31st Dec 2005 04:33 PM
Reference to Excel worksheet from Outlook form Long Nguyen Microsoft Outlook Form Programming 2 4th Feb 2005 04:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:23 PM.