Why do I have to prefix 'Excel' class with 'Microsoft.Office.Inter

B

Brian Kitt

I am working with a C# Form that will read data from an Excel worksheet. I
have done this many times in the past, but this is the first time I have done
this with Office 2003 (Office 11).

In the past, I could always do a 'Using Excel' and then access the
method/property/etc just fine.

With Office 2003 (Office 11) I must do Microsoft.Office.Interop.Excel in
front of every single method/property/etc. This does work fine, but makes my
program very wordy. I have tried adding 'using' statements, but to no avail.
I don't see why I can't just do 'Excel.Worksheet' rather than
'Microsoft.Office.Interop.Excel.Worksheet'.

Any advice is greatly appreciated. As I said, the program does work fine,
it's just wordy.
 
C

Cindy M.

Hi =?Utf-8?B?QnJpYW4gS2l0dA==?=,

The difference most likely comes from the fact that with Office 2003 you're
accessing PIAs, rather than IAs that Visual Studio has created for you (using
TlbImp.exe in the background). IAs are distributed as part of your solution with
the dlls, etc. PIAs are stored in the GAC and are therefore qualified
differently.

You should be able to condense your code by using statements along these lines
at the top of a class:

using xl = Microsoft.Office.Interop.Excel;

Then, everywhere you currently have the full namespace qualification, substitute
xl. You can test this by typing xl. (Dot): Intellisense should kick in at that
point and give you a list of valid commands.
I am working with a C# Form that will read data from an Excel worksheet. I
have done this many times in the past, but this is the first time I have done
this with Office 2003 (Office 11).

In the past, I could always do a 'Using Excel' and then access the
method/property/etc just fine.

With Office 2003 (Office 11) I must do Microsoft.Office.Interop.Excel in
front of every single method/property/etc. This does work fine, but makes my
program very wordy. I have tried adding 'using' statements, but to no avail.
I don't see why I can't just do 'Excel.Worksheet' rather than
'Microsoft.Office.Interop.Excel.Worksheet'.

Any advice is greatly appreciated. As I said, the program does work fine,
it's just wordy.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
B

Brian Kitt

That worked perfectly!! Thanks!!

Cindy M. said:
Hi =?Utf-8?B?QnJpYW4gS2l0dA==?=,

The difference most likely comes from the fact that with Office 2003 you're
accessing PIAs, rather than IAs that Visual Studio has created for you (using
TlbImp.exe in the background). IAs are distributed as part of your solution with
the dlls, etc. PIAs are stored in the GAC and are therefore qualified
differently.

You should be able to condense your code by using statements along these lines
at the top of a class:

using xl = Microsoft.Office.Interop.Excel;

Then, everywhere you currently have the full namespace qualification, substitute
xl. You can test this by typing xl. (Dot): Intellisense should kick in at that
point and give you a list of valid commands.


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)


This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top