Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Achieving Backward Compatibility with .NET Interop: Excel as Case Study
 »  Home  »  Windows Development  »  Interop  »  Achieving Backward Compatibility with .NET Interop: Excel as Case Study
Achieving Backward Compatibility with .NET Interop: Excel as Case Study
by Scott Rutherford | Published  07/23/2005 | .NET Framework Interop | Rating:
Scott Rutherford
Scott Rutherford is a consultant and .NET developer who has developed Enterprise business applications in many fields including Restaurant, Market Research, Automotive, and Analytical Chemistry. He is an Microsoft Certified Professional with the .NET Platform. 

View all articles by Scott Rutherford...
Using the Type Library

Problem: COM Automation across multiple COM versions

Microsoft Visual Studio .NET makes linking to COM components extremely easy. You simply right click "References" in the Project Explorer, and Add New Reference. You can then browse through the available .NET Assemblies and COM components to find the code library you wish to automate. Every COM interface available on the machine is described by a Type Library (commonly held in a TLB file, and referenced by a CLASSID in the Registry). When you add this reference to your project, the type library is wrapped with a .NET Interop assembly that allows you to use the COM interface at design-time, and then handles the calls to COM at run-time. When type libraries exist with multiple versions, this can complicate your life significantly.

Case: Microsoft Excel


The Add Reference dialog shows Excel 11.0 Object Library when Excel 2003 installed

As an example, we will look at Microsoft Excel. Depending on which version of MS Excel is loaded on the machine on which your project is compiled, you will get a different Interop created. If the project is then deployed to a machine with a different version of Excel, you can expect to run into problems. Microsoft has kept Office backward compatible for a few generations so you can expect most previous functionality to be available still in Office 2003 (aka 11.0). In a Production environment that spans multiple versions of office, however, you may still run into errors, because the structure of type libraries and how they are linked in the registry has changed. The PIA (Primary Interop Assembly) concept further complicates .NET deployment where backward compatibility is concerned.

[Visual Basic]
    Sub FillRange(ByRef rng As Excel.Range, ByVal iLength As Integer, ByVal iWidth As Integer)
        Dim saRet(iLength, iWidth) As Object
        ' ... fill array with values
        rng = rng.Resize(iLength, iWidth)
        rng.Value = saRet
    End Sub
VB code assigns a 2-dim String array to the Value property of an Excel.Range object

[System.Runtime.InteropServices.COMException] -2147352559 (80020011) Does not support a collection.
The above code produces this error when compiled on an Excel 2003 machine and run on Excel 2000

Generally, you can only install a single version of Office on a particular PC; various CLASSIDs that represent objects in the application are thus "installed", or "registered" to trigger this version. When a subsequent version is installed on the machine, previous versions are overwritten. Hence, Visual Studio will always only find one version to which it can link. In the case of PIAs, VS.NET will find a copy with a strong name, in the GAC, and hence will not even make an Interop assembly. Surprisingly, even if you explicitly link to a particular Type Library, VS.NET will override your reference and use the registered version.

Solution

You will find three distinct methods suggested to work around these problems:

  1. Build your project on different machines for compatibility with each respective version of Office.
  2. Install different versions of Office on your build machine (How-to).
  3. Use "Late Binding" to call whatever version of Office is available at run-time

Here, I suggest a fourth method: build to the lowest common denominator.

NOTE: Although this strategy was suggested by MS Support in Writing Automation Clients for Multiple Office Versions, it does not work with VS.NET 2003/5 and Excel 2003. VS.NET now updates all Interop references to match ActiveX libraries installed in the Registry--any manually created reference to an unregistered version is overwritten at compile time (except by the steps outlined below). MS Support now recommends one of the above 3 approaches. In fact, when I suggested this as a feature for the VS 2005 Beta version, it was deemed too complicated and excluded "By Design". The steps outlined below are surprisingly simple.

Find the lowest version with which the project will need to work in the Production environment and build to this. For advanced functionality only available on later versions this method can be extended--providing multiple Interop assemblies that will work in parallel. Additionally, this does not rely on having a particular version of the type library installed on the build machine--nor any version at all, for that matter. It does, however, require that you have access to the Type Library files in the version against which your project will build.

Step-By-Step

In order to build my project against Excel 2000, on a machine that has only Excel 2003 installed, I performed the following steps:

  1. Locate the Excel 9 (2000) Type Library
    • Normally, type libraries are named *.TLB
    • After a little googling, however, I found that the Excel 9 type library is called EXCEL9.OLB
    • I quickly found the EXCEL.OLB file on my (fully licensed and registered!!!) Office 2000 installation CD.
  2. Copy the Excel 9 Type Library to the build machine
  3. Create an Interop Assembly from the Excel9 Type Library
    • Choose a unique name that will distinguish YOUR Interop DLL from others that may be found
    • Use the .NET SDK Tool called TLBIMP.exe to build your Interop Assembly from the command line

     @echo off
     SET prog="C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\TlbImp.exe"
     %prog% EXCEL9.OLB /out=My.Excel9.Interop.dll
     pause
    
    DOS Batch commands to create the explicit Interop DLL using the TlbImp tool provided with the .NET SDK

  4. Link this new Interop Assembly to the VS.NET project, and build
    • Set the "Private" (aka "CopyLocal") property to True


    Browse... to the new Interop DLL in the Add Reference dialog

  5. Update code to reference the new Interop Library
    [Visual Basic]
        Sub FillRange(ByRef rng As My.Excel9.Interop.Range, ByVal iLength As Integer, ByVal iWidth As Integer)
    
  6. Deploy this Interop Assembly in the \bin directory along with the project
    • Your project will now work on any machine with Office 2000 or higher

References

.NET Framework Tools : Type Library Importer (Tlbimp.exe)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpgrftypelibraryimportertlbimpexe.asp

INFO: Writing Automation Clients for Multiple Office Versions
http://support.microsoft.com/kb/q244167

Working with the Office XP Primary Interop Assemblies
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_oxppias.asp

Office XP Primary Interop Assemblies Known Issues
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_piaissues.asp

Official Office 2003 Interop PIAs
http://support.microsoft.com/?scid=kb;en-us;897646&spid=2525&sid=global

INFO: Develop Microsoft Office solutions with Visual Studio .NET
http://support.microsoft.com/kb/q311452/

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.6060606060606 out of 5
 99 people have rated this page
Article Score48069
Related Articles
Attachments
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 08/08/2005)
Rating
Becuase this will only work for products like Excel and Word. It will not work with Outlook.
 
Comment #2  (Posted by an unknown user on 08/15/2005)
Rating
Worked exactly as described and solved my issue immediately.

Thank you.
 
Comment #3  (Posted by an unknown user on 08/15/2005)
Rating
Worked exactly as described and solved my issue immediately.

Thank you.
 
Comment #4  (Posted by an unknown user on 08/15/2005)
Rating
Backwards Compatibility is always a problem, great solution.
 
Comment #5  (Posted by an unknown user on 08/26/2005)
Rating
just what i needed.....
 
Comment #6  (Posted by Simon Garcia on 08/29/2005)
Rating
what about the 5.0 object library?, is it possible to use this one for all versions of excel from the 5.0 and above?
 
Comment #7  (Posted by an unknown user on 09/01/2005)
Rating
Great! Helped us out!
 
Comment #8  (Posted by Simone on 10/27/2005)
Rating
Excellent! Solved our issue. Thanks
 
Comment #9  (Posted by an unknown user on 03/01/2006)
Rating
awesome !!
 
Comment #10  (Posted by an unknown user on 04/07/2006)
Rating
Its clear, accurate and best of all it works !
 
Comment #11  (Posted by an unknown user on 05/12/2006)
Rating
Just one word: Thanks.
 
Comment #12  (Posted by an unknown user on 05/13/2006)
Rating
Extremely helpful. The exact solution that I have been looking for. Thanks.
 
Comment #13  (Posted by an unknown user on 05/25/2006)
Rating
Thhanks so much!!!
 
Comment #14  (Posted by an unknown user on 07/03/2006)
Rating
thx!!!

i love you -_-
 
Comment #15  (Posted by an unknown user on 07/10/2006)
Rating
Brilliant. Thank you!
 
Comment #16  (Posted by an unknown user on 07/26/2006)
Rating
A good simple solution and a good explanation of why
 
Comment #17  (Posted by an unknown user on 08/08/2006)
Rating
its the best article ive found about this issue and i've been searching for a long time! thanks
 
Comment #18  (Posted by an unknown user on 08/29/2006)
Rating
Very good on completeness, but your solution involves working on the client's PC, which is not an option for me
 
Comment #19  (Posted by an unknown user on 09/07/2006)
Rating
No need to access any client PC, but you do need to get hold of the correct TLB/OLB file. In addition to being available on the target PC, this is also on the Office installation disks and I'd imagine would not be too difficult to find online.
 
Comment #20  (Posted by an unknown user on 09/07/2006)
Rating
Excellent
 
Comment #21  (Posted by an unknown user on 09/22/2006)
Rating
Good and fast solution to our problems caused by several users having only Excel 2000 installed..
 
Comment #22  (Posted by Giacomo on 09/24/2006)
Rating
Good job.

But I've got quite a problem...
Those lines of code:
Dim aRange As Word.Range
aRange = WordApp.ActiveDocument.Paragraphs(1).Range

Result in an error: Interface 'My.MsWord9.Interop.Paragraphs' cannot be indexed because it has no default property.

This does not happen if I import Office 2003's namespace instead of Office 2000's one.
 
Comment #23  (Posted by an unknown user on 09/28/2006)
Rating
Thank you very much.I hope to help in other case.Keep the good work
 
Comment #24  (Posted by an unknown user on 10/26/2006)
Rating
Great job
 
Comment #25  (Posted by an unknown user on 11/13/2006)
Rating
Excellent step-by-step
 
Comment #26  (Posted by an unknown user on 12/28/2006)
Rating
Simple and effective solution
 
Comment #27  (Posted by an unknown user on 01/05/2007)
Rating
Thank you so much! I would have gone mad if I wouldn't have found this great article ...
 
Comment #28  (Posted by an unknown user on 01/14/2007)
Rating
Excellent article, Thank you for sharing this!!
 
Comment #29  (Posted by an unknown user on 01/25/2007)
Rating
thanks
 
Comment #30  (Posted by an unknown user on 02/06/2007)
Rating
Thanks very much !
Just what I needed
 
Comment #31  (Posted by an unknown user on 02/10/2007)
Rating
Worked well for me. Thank you.
 
Comment #32  (Posted by an unknown user on 02/19/2007)
Rating
In my case we don't have office in ourserver.do u have any idia how to register/use my excel dll.
 
Comment #33  (Posted by [The Author] on 02/20/2007)
Rating
You must install office on the server to use it.
The DLL you create here is only an INTEROP *wrapper* dll that allows you to call the Excel app vai .NET, it does not replace the Excel application.
 
Comment #34  (Posted by an unknown user on 03/20/2007)
Rating
I thought this was great until I tried to use it. I got an error message that said 'MSWORD9' is not a member of 'My'. How do I use it?
 
Comment #35  (Posted by an unknown user on 03/20/2007)
Rating
Thanks very much for this!

This works until I try and use the Excel9.Interop.Chart.Shapes.BuildFreeform method. Visual Studio then forces me to add a reference to office.dll:

Error 1 Reference required to assembly 'office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' containing the type 'Microsoft.Office.Core.MsoEditingType'. Add one to your project.

If I add the reference it asks for then I'm no longer able to use my code with previous versions of office it only works in office 12.

Any ideas on how to fix this?

 
Comment #36  (Posted by an unknown user on 03/20/2007)
Rating
Thanks very much for this!

This works until I try and use the Excel9.Interop.Chart.Shapes.BuildFreeform method. Visual Studio then forces me to add a reference to office.dll:

Error 1 Reference required to assembly 'office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' containing the type 'Microsoft.Office.Core.MsoEditingType'. Add one to your project.

If I add the reference it asks for then I'm no longer able to use my code with previous versions of office it only works in office 12.

Any ideas on how to fix this?

 
Comment #37  (Posted by an unknown user on 03/21/2007)
Rating
Outrageous that MS have dropped this from their own information - took me hours to find your excellent walkthrough, well done!
 
Comment #38  (Posted by an unknown user on 03/21/2007)
Rating
Very cool. Thank you!
 
Comment #39  (Posted by an unknown user on 03/22/2007)
Rating
SUPERB!!!! Well done!!!!!! You are the best
 
Comment #40  (Posted by [the author] on 03/23/2007)
Rating
RE: Comment #35 ... You may need to bulid interop for further DLLs, following the same instructions as above.
See a full list of Office OLBs in KB 249843 at this URL: support.microsoft.com/kb/249843

ER: Comment #34, careful to get the correct assembly name when calling TlbImp.exe and make sure the namespace you call in code is the same as you built.
 
Comment #41  (Posted by an unknown user on 03/26/2007)
Rating
Thank you for this really practical solution to a "PIA" problem. When this solution is implemented, I guess we don't need to install the PIAs anymore? Is this true? Seems to work without them and with them (excepting Office 2000)! I guess it uses the Excel directly.
 
Comment #42  (Posted by an unknown user on 03/26/2007)
Rating
Thank you for this really practical solution to a "PIA" problem. When this solution is implemented, I guess we don't need to install the PIAs anymore? Is this true? Seems to work without them and with them (excepting Office 2000)! I guess it uses the Excel directly.
 
Comment #43  (Posted by an unknown user on 04/16/2007)
Rating
Best solution on the web, thank you! One question though...I'm getting an error when running on the client computer. The client PC says the DLL is not registered. Any ideas?
 
Comment #44  (Posted by an unknown user on 04/16/2007)
Rating
Best solution on the web, thank you! One question though...I'm getting an error when running on the client computer. The client PC says the DLL is not registered. Any ideas?
 
Comment #45  (Posted by an unknown user on 05/06/2007)
Rating
You fourth solution is just what I needed. I will be thankfull for life.
 
Comment #46  (Posted by an unknown user on 05/09/2007)
Rating
Spent days on this issue before finding this solution which sorted my problem 100%
 
Comment #47  (Posted by an unknown user on 06/04/2007)
Rating
A million thanks - saved me loads of time!!

NB: I am using VS2005. I found that the compiler did not see "My.Excel9.Interop" in the source code. I had to use "Excel9.Interop" instead.
 
Comment #48  (Posted by an unknown user on 07/04/2007)
Rating
I was tearing my hair out trying to figure out how to support Word 2000, 2002, 2003 and 2007 and you solution is not only simple but it works! Thanks!
 
Comment #49  (Posted by an unknown user on 07/21/2007)
Rating
You are my savior :)
 
Comment #50  (Posted by an unknown user on 09/06/2007)
Rating
Extremely helpful!Could not figure out why it was running OK on my dev machine which had excel 2003 and not on the server
 
Comment #51  (Posted by thomas on 09/12/2007)
Rating
I did the same thing some years ago, but a lot simpeler :

- i installed office 97 on the development machine (anyone has a cd of that somewhere, it is installed in 5 minutes even if you have to uninstall the current version)
- add the reference to word/excel (8.0 object library, which is then available in the COM list)
- in my \bin directory, I copy the created interop dll's (interop.excel.dll, interop.word.dll, interop.vbide.dll, interop.office.dll)
- i add these interop dll files to the project in a separate folder (the "_Dll" folder in my project)
- I remove the references to word and excel 97
- i add references to the local copy's of the interop dll's

Hup, now I can develop against office 97, even when older versions of office are installed on the development pc.
It works on all versions of office starting from 97, on all computers. 100's installed, no problems, advanced interop functionality in word, excel and outlook ...

In essence, you just have to add the interop dll's from the earliest version you want to support to your project, and add references to them instead of the 10.0, 11.0 etc versions in the COM references list.
 
Comment #52  (Posted by Abdalla on 09/29/2007)
Rating
Excellent work dude.. Googled everywhere but could not get the solution..better than late binding..
 
Comment #53  (Posted by an unknown user on 10/07/2007)
Rating
i faced the problem of working with multiple solutions and it provided me the best possible solution.
 
Comment #54  (Posted by an unknown user on 10/12/2007)
Rating
Good Work, I need this advice
 
Comment #55  (Posted by an unknown user on 10/15/2007)
Rating
Worked just perfectly. Excellent. Thanks a lot
 
Comment #56  (Posted by an unknown user on 11/26/2007)
Rating
Saved my life. Thanks.
 
Comment #57  (Posted by an unknown user on 11/26/2007)
Rating
Saved my life. Thanks.
 
Comment #58  (Posted by an unknown user on 01/08/2008)
Rating
Excellent solution
 
Comment #59  (Posted by an unknown user on 01/09/2008)
Rating
Great solution - this one had me tearing my hair out. 5 out of 5 for me!
 
Comment #60  (Posted by an unknown user on 01/11/2008)
Rating
Great Article, very well explained and solved my deployment problem.
 
Comment #61  (Posted by an unknown user on 02/29/2008)
Rating
Why Microsoft doesn't care with that kind of issue ?
Just wanna say thank you so much giving us this late binding solution.
Great solution... I'm just wondering how to apply it to Outlook...

 
Comment #62  (Posted by an unknown user on 04/03/2008)
Rating
I didn't find this information in any other web site. Just what I needed. Thanks!!
 
Comment #63  (Posted by an unknown user on 04/09/2008)
Rating
Solved my issue very well!!!!!


 
Comment #64  (Posted by an unknown user on 04/18/2008)
Rating
very helpful
 
Comment #65  (Posted by an unknown user on 06/23/2008)
Rating
Very helpful--this was easy and effective!
 
Comment #66  (Posted by an unknown user on 10/21/2008)
Rating
After several hours of "googling" I found your answer, and sounds like heaven to me. Clear, plain, easy, perfect!. Thanks. Vicente
 
Comment #67  (Posted by an unknown user on 10/21/2008)
Rating
What about compatibility between 2003 and 2007. I can't find an OLB for 2003 version, it seems like they have moved everything to Excel.exe. Any ideas?
 
Comment #68  (Posted by an unknown user on 10/21/2008)
Rating
What about compatibility between 2003 and 2007. I can't find an OLB for 2003 version, it seems like they have moved everything to Excel.exe. Any ideas?
 
Comment #69  (Posted by an unknown user on 12/02/2008)
Rating
erdelacta
 
Comment #70  (Posted by an unknown user on 12/05/2008)
Rating
I'm using Excell 11 libray to run on a xp machine with Office 2000 however it works perfectly if the user is administrator of the machine so I don't see any trouble using 11.0 interropt on a machine with office 2000 so what's the deal ?
 
Comment #71  (Posted by an unknown user on 12/19/2008)
Rating
GREAT SOLUTION!!!!!!
In few minutes my application became compatible with EXCEL2000...wonderfull and straight forward!
Thanks a lot.
 
Comment #72  (Posted by an unknown user on 12/19/2008)
Rating
GREAT SOLUTION!!!!!!
In few minutes my application became compatible with EXCEL2000...wonderfull and straight forward!
Thanks a lot.
 
Comment #73  (Posted by an unknown user on 01/23/2009)
Rating
Thanks for the solution and the detailed description!!
 
Comment #74  (Posted by an unknown user on 03/25/2009)
Rating
thanks this was hard to find but saved my project
 
Comment #75  (Posted by an unknown user on 03/27/2009)
Rating
I tryied it and worked on Windows XP, but in W2K3 server doesnt, can you explain me why?
 
Comment #76  (Posted by an unknown user on 08/04/2009)
Rating
Works like a charm, thanks.
 
Comment #77  (Posted by an unknown user on 08/04/2009)
Rating
Works like a charm, thanks.
 
Comment #78  (Posted by an unknown user on 12/17/2009)
Rating
Thanks for the solution and the detailed description.

 
Sponsored Links