9 Replies - 3050 Views - Last Post: 12 January 2011 - 01:46 PM Rate Topic: -----

#1 hookiethe1   User is offline

  • D.I.C Lover

Reputation: 416
  • View blog
  • Posts: 1,336
  • Joined: 28-September 10

Interop, Excel, Rangenames

Posted 12 January 2011 - 08:16 AM

G'day all, I'm trying to read in an Excel file using interop, and while get_Value does a great job of reading the spreadsheet data, it doesn't seem to get Range names. This is a problem as the particular spreadsheet I need to read has each column labeled as a range with a name, and these are the only unique and reliable identifiers.

In VB.NET I was able to get column indexes from range names with:

someinteger = worksheet.Range("rangename").column


(I know the names, the column index is what I need)

So far I've been unable to find a C# equivalent for the above line of code, and the MSDN library's treatment of Range is pretty dismal. Anybody got any ideas?

Thanks a bunch

Is This A Good Question/Topic? 0
  • +

Replies To: Interop, Excel, Rangenames

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Interop, Excel, Rangenames

Posted 12 January 2011 - 08:31 AM

C# or VB.NET, the methods should be the same. At most, I would think that maybe Range is a dictionary, and as such you would use [ ] instead of ( ) for the index.
Was This Post Helpful? 1
  • +
  • -

#3 hookiethe1   User is offline

  • D.I.C Lover

Reputation: 416
  • View blog
  • Posts: 1,336
  • Joined: 28-September 10

Re: Interop, Excel, Rangenames

Posted 12 January 2011 - 09:23 AM

That's what I thought, but I get a red squiggle under Range and a missing using directive or assembly reference error. But I have Microsoft.Office.Tools.Excel.v9.0 reference and using Microsoft.Office.Tools.Excel as per MSDN. Maybe I am still missing something, will keep on looking.

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Interop, Excel, Rangenames

Posted 12 January 2011 - 09:47 AM

Probably missing Microsoft.Office.Interop.Excel. I'm not an expert on interop, but I know you need that one.

Also, Excel 9? Isn't 2010 like Excel 14? So that would make the version you're using...from Office 2000? Might be time to update :P

This post has been edited by insertAlias: 12 January 2011 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#5 hookiethe1   User is offline

  • D.I.C Lover

Reputation: 416
  • View blog
  • Posts: 1,336
  • Joined: 28-September 10

Re: Interop, Excel, Rangenames

Posted 12 January 2011 - 09:58 AM

No, I have interop.Excel, and according to MSDN, for visual studio 2008 and Excel 07 it's Tools.Excel.v9.0. I've double and triple checked all of that, still no joy.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Excel;


Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Interop, Excel, Rangenames

Posted 12 January 2011 - 10:11 AM

Well, I don't really know what to tell you. Maybe this will help:

http://msdn.microsof...e(v=vs.80).aspx
Was This Post Helpful? 1
  • +
  • -

#7 hookiethe1   User is offline

  • D.I.C Lover

Reputation: 416
  • View blog
  • Posts: 1,336
  • Joined: 28-September 10

Re: Interop, Excel, Rangenames

Posted 12 January 2011 - 10:17 AM

Thanks, I've read that page about a dozen times this morning. It's gotta be some stupid simple thing.
Potentially stupid question: an assembly is the same as a reference, right?
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Interop, Excel, Rangenames

Posted 12 January 2011 - 10:32 AM

Well, you add a reference to an assembly.
Was This Post Helpful? 0
  • +
  • -

#9 hookiethe1   User is offline

  • D.I.C Lover

Reputation: 416
  • View blog
  • Posts: 1,336
  • Joined: 28-September 10

Re: Interop, Excel, Rangenames

Posted 12 January 2011 - 12:08 PM

Guess I should have been clearer, I meant is "adding a reference" the same as "adding a reference to an assembly." Thanks for clearing that up.

This sure is frustrating, should have called the thread "how to waste half a day (so far) on a single line of code."
Was This Post Helpful? 0
  • +
  • -

#10 hookiethe1   User is offline

  • D.I.C Lover

Reputation: 416
  • View blog
  • Posts: 1,336
  • Joined: 28-September 10

Re: Interop, Excel, Rangenames

Posted 12 January 2011 - 01:46 PM

So here's how it goes:
Excel.Range range = worksheet.get_Range("rangename", Type.Missing);
int index = range.Column;


Virtually the same as VB.NET! :wacko:

This post has been edited by hookiethe1: 12 January 2011 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1