C# automating Office: supporting both Office 2003 and XP

M

Mark Allsop

Hi,

I have a C# program, written using VS.NET 2003, which automates Word & Excel
to perform some tasks for me. I previously had it working with the Office XP
primary interop assembly. The code needs to work on machines with both
Office XP & 2003.

I am finding reference errors: when I add a reference to Excel on a PC with
Office 2003, it works on that PC fine. I then open the project on the Office
XP PC, and I get a reference error for Excel & Word.

I presume this is due to the version of the interop assemblies?

How do I add a reference to Word & Excel, which will work on PC's with
either Office XP or 2003?

Can I:
- use the Office XP interop assemblies, without losing functionality
when I'm on an Office 2003 machine?
- use the GAC somehow to just use whichever version is on a particular
PC?
- do I have to code to a particular version of Office?
- are the Office 2003 interop assemblies backward compatible? (ie. load
the Office 2003 interop assemblies onto a machine with Office XP?)

Any tips would be most appreciated.

Thanks,
+Mark.
 
S

scorpion53061

Do you have references to 10.0 Office object librarys as well as the Office
2003 object library? When you installed office 2003 did you leave office xp
on your computer?
 
M

Mark Allsop

No- Office 2003 was installed onto a clean system.

Part of the problem is for the .NET code to compile and work on systems,
regardless of the version of Office installed, or previously installed,
provided the interop assemblies for the particular version of Office are
always present.

Thanks,
+Mark.
 
S

scorpion53061

The reason I asked is that if you attempt to run code against office xp and
never had it installed (i have yet to install 2003 - getting it this week)
the object library for xp will be missing and thus the code will be unable
to execute.
Install XP on your development machine, add the 10.0 reference and it will
work.
 
M

Mark Allsop

Yes, I know if I install Office XP it will work today, I loaded Office 2003
only to specifically check that it will work on both Office XP and Office
2003.

My problem is, what happens when I ship this to someone who in the future
only has Office 2003? I intend to ship the exe, as well as the code, so both
need to be Office version agnostic.

How do I set up references so it just picks whatever version of Office
happens to be installed?

+Mark.
 
H

Howard Kaikow

The standard practice, within Office and VB, for Automating an Office app
is to compile using ONLY the earliest version of the relevant library.

Compiling with the earliest PIA would be consistent with past
recommendations for multiple version Office apps in, say, VB.
If the PIAs do not work the same way, then there may be a problem that MSFT
needs to quickly fix.
 
S

scorpion53061

Ship all office libraries (9, 10, and what Office2003 uses) just in case.
Of course Office 2003 centric features will cause exceptions so I know where
you are at....
 
R

Robert Jacobson

If you mean "ship the Office type libraries," those aren't redistributable.

As Howard says, Mark needs to develop using the oldest version of Office
that he intends to support. All of the type libraries are forward
compatable -- an application developed against the Office 10 (XP) type
libraries should be compatable with Office 2003.

If Mark needs support for Excel XP and Excel 2003, he should just install
Excel XP on his development machine and use the Excel XP PIA's. He should
have a separate test machine that runs Excel 2003, just to be certain that
it works OK.
 
M

Mark Allsop

Herein lies the problem.

It doesn't work (unless I've made a silly mistake in how I've added the
references- I added a reference to the Excel COM object, which in turn added
a reference to the pia in the global assembly cache). Using SourceSafe to
get the code on two different PC's, the code with the reference to Office XP
pia's doesn't compile on a PC with only Office 2003, and vice versa.

It seems to work if I extract MS' XP pia's from their web site, put them in
the same directory on each machine, and add a reference directly by browsing
to the .dll file to add the reference. I'll use that for the time being.

I'd love to know how I'm meant to do this using the GAC, and as you
mentioned, referencing the oldest version of Office. I'm really surprised
that doesn't work- is there something I've missed (MS- would love you to
confirm) in how to add a reference to Office which compiles regardless of
Office XP or Office 2003?

fyi, I've also found that the signature of Word's document .Open() has added
an extra parameter in Office 2003, so in particular under C# where there are
no optional parameters, the code needs to slightly change for Office 2003
pia's to allow for this extra parameter.

+Mark
 
R

Robert Jacobson

I'm not an expert on the PIA's. I'm developing for Office 2000 and higher,
so I don't get to use those darn newfangled PIA's. <g> However, my Word
addin works fine with Word 2000, XP and 2003.

Are you sure that you're referencing the XP (Office 10) PIA's? It's
possible that installing Office 2003 on your dev machine, or using
SourceSafe, could have mucked things up somehow.

I don't understand why your code would need to change because of the change
in signature definition -- if you want Word XP and higher compatibility,
your code should only reference the XP PIA. Your code should match the
signature for the Word XP method, and it should run (without modifications)
using Word 2003.

I'd try creating a simple test project (like a simple "Hello World" add-in)
that references Office XP, and then deploy it (using the Setup and
Deployment project) to a test machine that uses Office 2003. It should
automatically deploy the XP interop library with the project, and it should
run without any problems in Office 2003.

Just to reiterate, it's better not to have Office 2003 installed on your dev
machine. Pretend that your computer is frozen in time in 2002 -- install
Office XP and the Office XP PIA's, but nothing related to Office 2003. (Try
a reformat and reinstall of everything, if nothing else works.) That way
your app should have perfect compatibility with Office XP and higher.
 
H

Howard Kaikow

They may be forward compatible, but one still needs to test in all versions
as some things break in various apps when moving to a more recent version.
 
H

Howard Kaikow

There is also the problem mentioned in of the readme that the PIAs need to
be re-installed into the GAC if certain things happen on a system, e.g., I
believe updating an Office app my cause this problem.

MSFT does need to publish a KB or other, article addressing multiversion
Office with .NET.
 

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