How do I access different versions of Excel from one .NET executable?

J

John Brock

I want to use Visual Studio (2008, writing in VB.NET) to create an
EXE file that will, among other things, read from and write to
Excel workbooks. In particular, I want the program to work whether
the machine has Office 2003 or Office 2007 installed. (Other
versions of Excel would also be nice -- in particular 2010, when
it comes out -- but those two are critical).

I have no trouble accessing Excel 2007 on my own PC; however when
I copy the EXE file to a PC with Office 2003 installed the program
fails when it hits the Excel access code. (Note that VB.NET programs
compiled on the other PC are able to read and write Excel 2003
files on that machine, so that's not the problem).

I do know something about PIAs, although perhaps not enough. My
project makes a COM reference to the Microsoft Excel 12.0 Office
Library, which allows it to access Excel 2007. I believe that to
access Excel 2003 I would need to reference the 11.0 library, which
doesn't exist on my machine, so I can't reference it. Even if it
did, I don't know if it's possible or would make sense to reference
both libraries in the same project.

So what do I do? My requirement seems very straightforward --
something that a lot of people would want to do. But I've looked
around a bit without finding a way to do it. Indeed, I've found
disturbing suggestions that I will need to compile a separate
version of my program for each version of Office I want to be
compatible with, which would be very unpleasant. (Just for starters,
since you can't have two versions of Office on the same machine,
I would need to get a second PC just for this!).

I am really hoping that Microsoft has not made my life that difficult.
So can anyone give me some advice, or point me to documentation
that might help me out?
 
A

Andreas Johansson

Usually it is most easy to install the earliest version of excel you want to
use on the development machine. Then reference that version instead in your
project.

You might be able to install them side by side so no need to uninstall 2007.
 
P

Patrick Molloy

you can have multiple versions of Excel installed....and you'll then have
access to each Excel library.
your code should be able to instantiate Excel, and use the relevant library.
 
J

John Brock

Hmmm..., are you *sure* you can have two (or more) versions of
Office on the same machine? I could swear that I read somewhere
this isn't possible.

Anyway, to be certain I understand, you are saying that once I
install both versions of Office (with both sets of PIAs), that my
project should reference *both* the 11.0 and 12.0 versions of the
Microsoft Excel Office Library, and the appropriate reference will
be used depending on what is on a particular machine?

But what will happen on *my* machine? The VB code that accesses
Excel doesn't specify a version, so if both versions are available,
which will be instantiated?

Also, what happens to my file associations if I have two versions
of Office? I'm pretty nervous about taking this route. Maybe
there is some way I can install and reference the Office 2003 PIAs
without actually installing Office 2003?
 
J

John Brock

OK, you are saying that if my program references the Office 2003
PIA (Microsoft Excel 11.0 Object Library) this will work with later
versions of Excel, but not the reverse. This is actually the
opposite of what I would have expected. Any idea what is going on
here? And is this likely to hold when Office 2010 comes out.
 
P

Patrick Molloy

i didn't say OFFICE, I said Excel
I run Excel 2003 and Excel 2007
When installing Office, there's an option that allows you to keep older
versions.
 
T

Tom [Pepper] Willett

But yes, you can have two different versions of *Office*, but not Outlook.

:i didn't say OFFICE, I said Excel
: I run Excel 2003 and Excel 2007
: When installing Office, there's an option that allows you to keep older
: versions.
:
:
: : > Hmmm..., are you *sure* you can have two (or more) versions of
: > Office on the same machine? I could swear that I read somewhere
: > this isn't possible.
: >
: > Anyway, to be certain I understand, you are saying that once I
: > install both versions of Office (with both sets of PIAs), that my
: > project should reference *both* the 11.0 and 12.0 versions of the
: > Microsoft Excel Office Library, and the appropriate reference will
: > be used depending on what is on a particular machine?
: >
: > But what will happen on *my* machine? The VB code that accesses
: > Excel doesn't specify a version, so if both versions are available,
: > which will be instantiated?
: >
: > Also, what happens to my file associations if I have two versions
: > of Office? I'm pretty nervous about taking this route. Maybe
: > there is some way I can install and reference the Office 2003 PIAs
: > without actually installing Office 2003?
: >
: > In article <[email protected]>,
: >>you can have multiple versions of Excel installed....and you'll then
have
: >>access to each Excel library.
: >>your code should be able to instantiate Excel, and use the relevant
: >>library.
: >>
: >>: >>> I want to use Visual Studio (2008, writing in VB.NET) to create an
: >>> EXE file that will, among other things, read from and write to
: >>> Excel workbooks. In particular, I want the program to work whether
: >>> the machine has Office 2003 or Office 2007 installed. (Other
: >>> versions of Excel would also be nice -- in particular 2010, when
: >>> it comes out -- but those two are critical).
: >>>
: >>> I have no trouble accessing Excel 2007 on my own PC; however when
: >>> I copy the EXE file to a PC with Office 2003 installed the program
: >>> fails when it hits the Excel access code. (Note that VB.NET programs
: >>> compiled on the other PC are able to read and write Excel 2003
: >>> files on that machine, so that's not the problem).
: >>>
: >>> I do know something about PIAs, although perhaps not enough. My
: >>> project makes a COM reference to the Microsoft Excel 12.0 Office
: >>> Library, which allows it to access Excel 2007. I believe that to
: >>> access Excel 2003 I would need to reference the 11.0 library, which
: >>> doesn't exist on my machine, so I can't reference it. Even if it
: >>> did, I don't know if it's possible or would make sense to reference
: >>> both libraries in the same project.
: >>>
: >>> So what do I do? My requirement seems very straightforward --
: >>> something that a lot of people would want to do. But I've looked
: >>> around a bit without finding a way to do it. Indeed, I've found
: >>> disturbing suggestions that I will need to compile a separate
: >>> version of my program for each version of Office I want to be
: >>> compatible with, which would be very unpleasant. (Just for starters,
: >>> since you can't have two versions of Office on the same machine,
: >>> I would need to get a second PC just for this!).
: >>>
: >>> I am really hoping that Microsoft has not made my life that difficult.
: >>> So can anyone give me some advice, or point me to documentation
: >>> that might help me out?
: >>> --
: >>> John Brock
: >>> (e-mail address removed)
: >>>
: >
: >
: > --
: > John Brock
: > (e-mail address removed)
: >
 
J

Jesse Houwing

Hello John,

Yes that is the case. You cannot reference the later PIA because these contain
new functionality not yet available when the previous version came out. But
the old excel functions are still built into the new executables, so the
older PIA will work regardless of later upgrades. Until MS decides to deprecate
such functions in later versions

Knowing that we'll have a 64 bit version of the next Office, I guess that
there will be a break in backwards compatibility as I doubt that a 32 bit
PIA for Excel 2003 would be able to load Excel 2010 64 bit. It should still
work with the 32 bit version though. (not verified nor tested)

Jesse
 
J

John Brock

It is possible (but not supported).

However, the replies you're getting seem mostly directed at "Can I have both
installed on one PC" and your question is more about "Can I develop in Visual
Studio for both on one PC?"

My understanding is that you can't (or that you'd need to do at least half your
dev in a virtual machine). I'd bring up the same questions in a group devoted
to .NET/Visual Studio/VSTO.

Can you suggest such a group? I didn't see anything like that on
Usenet.

Also, do you have any idea whether it is possible to install the
Office 2003 PIAs on my machine, in addition to the PIAs that came
with Office 2007, without actually installing Office 2003? It
appears from what folks are saying that the earlier PIAs will work
with later versions of Office, so if I could use those that would
be the simplest thing.

Of course, in the same vein, I could always uninstall Office 2007
and install Office 2003. That would be sad, but it would still
beat having to buy another machine, or keep a duplicate code base
on a virtual machine. :-(
 
J

John Brock

Very interesting!!! I knew about reflection and late binding, but
it didn't occur to me that they could be used for this. (I assume
this method will work just as well for VB.NET as for C#)?

Yes, coding would be ugly. I would especially miss Intellisense.
But it looks like doing it this way would remove all version worries.
(I'm not doing anything especially sophisticated with Excel, so
whatever I need is going to be there in all versions). In addition,
I wouldn't even have to worry about whether any PIAs were installed
on the end user's machine (which is not guaranteed). Have I
understood all this correctly?

In practice I think I would end up coding everything first using
the Excel PIA, just to see what all the calls ended up looking
like, and then translating everything to late binding. Yes, ugly,
but maybe the best way. Thanks!
 
J

Jack Jackson

Can you suggest such a group? I didn't see anything like that on
Usenet.

Also, do you have any idea whether it is possible to install the
Office 2003 PIAs on my machine, in addition to the PIAs that came
with Office 2007, without actually installing Office 2003? It
appears from what folks are saying that the earlier PIAs will work
with later versions of Office, so if I could use those that would
be the simplest thing.

Of course, in the same vein, I could always uninstall Office 2007
and install Office 2003. That would be sad, but it would still
beat having to buy another machine, or keep a duplicate code base
on a virtual machine. :-(

If you go to <http://www.microsoft.com/downloads/en/default.aspx> and
search for 'office pia' there appear to be downloads for Office XP,
Office 2003 and Office 2007.
 
W

Wilson, Phil

If you're doing simple things you should be fine. That's what got me
started - all I wanted to do was send Outlook mail, and the late-binding to
do all that has been around for years without PIAs.
 
Joined
Apr 24, 2010
Messages
1
Reaction score
0
John, did you ever get anywhere with this? I have the exact same problem - but with a vb.net windows application in 2.0. I have Office 2007 on my machine but when I try to install it on a machine with Office 2003 I have to change all my references. My initial thought was to make two different versions of my application, and maybe that's the easiest way to go. Then when a new Office comes out I make a new version. However, if there was an app.config approach it would be much simpler to change, or even have the ability to allow the user to select their Office version when installing the application.

Please let me know!
Thanks!
 

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