Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7730737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:18:04+00:00 2026-06-01T06:18:04+00:00

I have some existing custom Excel workbooks/applications with code-behind C#, which work fine. They

  • 0

I have some existing custom Excel workbooks/applications with code-behind C#, which work fine. They do things like referencing Tables defined in the active worksheet, setting their datasources to IEnumerables, etc.

However, now I need to reference a Table which is not in the active sheet. Basically, the active sheet has a button that triggers some C# code-behind, and I need to figure out how to get a reference to a Table on the fourth tab/worksheet in my workbook while inside the code-behind for the first tab/worksheet (the active one).

In my existing code (which works), I can do this on the active sheet:

this.Table1.DataSource = results;
this.Table1.RefreshDataRows();

… so the “Table1” reference is right there and I can access it easily.

However, trying to get a reference to a Table in the other sheet, I run into ComObject errors. For example, I was hoping that this (or something like it) would work:

((Sheet4)Application.Worksheets[3]).Table2.DataSource = results;
((Sheet4)Application.Worksheets[3]).Table2.RefreshDataRows();

… but that throws the error: “Cannot convert type ‘System.__ComObject’ to ContractStatementClient.Sheet4′”. Writing those lines allows Visual Studio to find the Table (Table2) using IntelliSense, but at execution time it fails with the ComObject error I mentioned. “Sheet4” is the name of the class that Visual Studio created for me when I made the new tab.

I can get references to Worksheets that seem to work by doing:

Application.ActiveWorkbook.Worksheets[3]

… but I can’t seem to figure out how to get from there to having a handle to a Table (or ListObject), that I can set the DataSource property on, as I’ve done in the past.

I tried this, but it throws a “NOINTERFACE” COM error during the cast:

Worksheet worksheet = Application.ActiveWorkbook.Worksheets[3];
((Microsoft.Office.Tools.Excel.ListObject) (worksheet.ListObjects[1])).DataSource = results;

Am I missing something obvious? I know next to nothing about COM, so it’s tough for me to diagnose what’s going on. In the end, I just need to figure out how to set the datasource for the Table/ListObject in the other tab/worksheet (and presumably tell it to refresh itself).

Thanks! Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:18:06+00:00Added an answer on June 1, 2026 at 6:18 am

    I ended up using the following approach, which worked quite well.

    At the workbook level, I added a static array of type “WorksheetBase”, and instantiated it in the “startup method for the Workbook, like this:

    public static WorksheetBase[] StoredWorksheets;
    
    private void ThisWorkbook_Startup(object sender, System.EventArgs e)
    {
        StoredWorksheets = new WorksheetBase[5];
    }
    

    Then, in each individual worksheet, I added this to the “startup” method:

    private void Sheet1_Startup(object sender, EventArgs e)
    {
        ThisWorkbook.StoredWorksheets[0] = this;
    }
    

    Each worksheet knows which zero-based index it falls in, so this particular sheet uses index == 0, whereas the second sheet would use index == 1.

    Now, when I want to access a specific member of another worksheet anywhere in my client, I reference the appropriate index, cast to the appropriate type for the sheet I want, and go to town:

    ((Sheet4)ThisWorkbook.StoredWorksheets[3]).Table2.DataSource = results;
    ((Sheet4)ThisWorkbook.StoredWorksheets[3]).Table2.RefreshDataRows();
    

    I’m sure there is a more straightforward way to do this using the COM back-end, but darned if I could figure it out. Hopefully this helps somebody else save some time!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a custom ErrorProvider which adds some functionality to the existing ErrorProvider
I have some existing code which isn't formatted consistently -- sometimes two spaces are
I have the following code which sets up some custom form elements on the
I have upgraded some VB6 code, which uses fixed length strings in custom types,
I have some existing code that retrieves data from a database using ADO.NET that
I have to analyse some existing Erlang code. Does anybody knows about a tool
Some existing web services I consume have methods that look something like this: List<Employee>
I have some classes inherit from existing Windows Controls like TextBox and DateTimePicker, ..etc
I have a custom exception class which contains some additional fields. I want these
I am new to R development, and have to modify some existing code. Specifically,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.