We are creating a Windows Form application (C# or VB.NET) that needs to reference an Office 2003 or Office 2007 COM object, depending on the version of office installed. What is the best way to handle this scenario and reference the correct COM object at runtime?
views:
316answers:
2Would the Primary Interop assemblies for Office not help with this? I don't know for sure as I haven't had to use them in earnest, but I think they would.
Hi,
unless you want to use any of the newly added objects and methods of the Office 2007 object model, it is fine to build referencing the Office 2003 PIAs, just make sure the correct version of the PIAs is deployed on the target system:
Another way around this problem is to remove the dependency on the later PIAs. Because of the high degree of backwards compatibility in Office, you can safely assume that if your add-in works on Office 2003 (with the Office 2003 PIAs), then it should also work on Office 2007 (with the Office 2007 PIAs).
(from Add-ins for Multiple Office Versions without PIAs by Andrew Whitechapel)
Otherwise I would recommend you the following blog articles by Andrew Whitechapel: Can you build one add-in for multiple versions of Office? (See the BIG warning that this is not officially supported by Microsoft).
Another option where you do not need the PIAs (this makes deployment a lot easier) would be to use ComImport together with late binding. This is however slower than using the interop assemblies, but if the automation code is not on the fast path this might be a good solution. You'll find an explanation how to implement this in the same blog post: Add-ins for Multiple Office Versions without PIAs