Solved

Accessing the array of Worksheets in a Workbook

Posted on 2011-03-17
2
941 Views
Last Modified: 2013-12-17
I am developing a Visual Studio Tools for Office using Visual Studio 2008.  This is an Excel VSTO add-in, which means that it operates in the same process in as Excel.  It also adds a couple of buttons to the Excel ribbon.  That works fine.  What I am having problems with is accessing one of several Worksheets in a Workbook.  Here is how I get the Workbook:

Microsoft.Office.Interop.Excel.Application a1 = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Microsoft.Office.Interop.Excel.Workbook w1 = (Microsoft.Office.Interop.Excel.Workbook)a1.ActiveWorkbook;

Now, I can get get the ActiveSheet like this:
Microsoft.Office.Interop.Excel.Worksheet wsa = (Microsoft.Office.Interop.Excel.Worksheet)w1.ActiveSheet;

That works fine.  I can also access the count of the number of Sheets like this:
int i = w1.Sheets.Count;

However, if I try to access one of the worksheets using an array syntax, It gives me any exception:
Microsoft.Office.Interop.Excel.Worksheet wsa = (Microsoft.Office.Interop.Excel.Worksheet)w1.Sheets[0];

With this, I get the following exception:
0x8002000b, Invalid Index, DISP_E_BADINDEX

The thing is, I have previously done the same thing in another application using Microsoft Office Automation (the application is in a different process), and it worked.  It would seem that for some reason, the COM marshalling is not properly supporting using the array mechanism.  Can anyone tell me how to randomly access my WorkSheets from the Workbook?
0
Comment
Question by:thomehm
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
Comment Utility
I think Workbook.Sheets is 1-based not 0-based, so to get the first Worksheet it should be:

Microsoft.Office.Interop.Excel.Worksheet wsa = (Microsoft.Office.Interop.Excel.Worksheet)w1.Sheets[1];
0
 

Author Closing Comment

by:thomehm
Comment Utility
So obvious, now that I think of it.  But I just starred past that possibility!  Thanks!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now