0

There are different office version, different com type libraries for managing them.

Could anyone tell me how should i write the code to operate on different Excel version? What about PIA for office, should I include them all with the project?

How to solve the problem of office multiversioning?

1
  • Just checking: I hope you're not using this in ASP.NET or another server-side application? Commented Jan 5, 2012 at 20:53

2 Answers 2

4

This is not fundamentally different from writing a .NET program and expecting it to run on windows 98 as well as Windows 7. The first choice is obvious, you'll need to target .NET 2.0 or it isn't going to run on 98. Same with Office, pick the lowest version you want to support.

Microsoft has done a terrific job keeping the COM interop for Office backwards compatible, odds are good that a user with a newer version of Office can use your program without problem. This of course is not a guarantee, you need to test it. Which in itself should be a guide to what version of Office you're willing to support. It better be one that you have access to. An MSDN subscription is a good way to get different versions.

Using late binding as suggested by Wiktor is the last thing you want to do. It doesn't stop you from accidentally using an interface that isn't available in an earlier version.

Sign up to request clarification or add additional context in comments.

Comments

3

1) take advantage of the dynamic binding

2) stick with late binding

and your code will work on any version.

// work with any version
Type wordType = Type.GetTypeFromProgId( "Word.Application" );

dynamic wordApp = Activator.CreateInstance( wordType );

// late binding + dynamics - always works
wordApp.Visible = true;

4 Comments

Are you 100% sure it will work on all office version? How will I know which methods I may run on such created type? Do you have some kind of examples of dynamic/late binding?
Yes, I am sure. COM interop on dynamics uses late binding, the feature was introduced in C#4. Late binding works on all properties/methods.
How does it work if there are several PIA's on the machine? Will it take the one which has the method I am calling?
It works directly at the COM level. PIAs has nothing to do with this technique.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.