问题
I have a WinForms application written in C# which auto generates letters in Word. The application works fine with client machines that have Office 2010 installed, but not with those machines with Office 2007 installed, when instead I get the following error message -
could not load file or assembly 'Microsoft.Office.Interop.Word version=14.0.0
I suspect I need to Add a Reference to an older version of Interop.Word e.g. version=12, but there is no such option in the list of references available to be via Visual Studio.
Am I right about needing an older reference and if so where can I locate this?
Or if there is something else I need to consider please advise.
回答1:
Note that .NET framework 4.0 has support for embedded Primary Interop Assemblies (PIA).
You can enable it on the Microsoft.Office.Interop.Word reference by setting the 'Embed Interop Types' to 'True'.
See http://msdn.microsoft.com/en-us/library/dd997297(v=vs.100).aspx for more information about this.
Another way to support multiple Office versions is to use a library like this:
http://netoffice.codeplex.com/
The library uses late-binding calls via COM interop and does not require the use of the Primary Interop Assemblies at all.
回答2:
Yes you should refrence the 2007 PIAs since they will work against both Office 2007 and Office 2010. In general you should reference the PIAs for the oldest version of Office that you will support.
You can download the Office 2007 PIAs from here
From MSDN:
When you install and register the Office PIAs in the global assembly cache (either with Office or by installing the redistributable package for the PIAs), the binding redirect assemblies are also installed only in the global assembly cache. These assemblies help make sure that the correct version of the primary interop assemblies are loaded at run time. For example, when a solution that references a 2007 Microsoft Office primary interop assembly runs on a computer that has the Microsoft Office 2010 version of the same primary interop assembly, the binding redirect assembly instructs the .NET Framework runtime to load the Microsoft Office 2010 version of the primary interop assembly.
来源:https://stackoverflow.com/questions/13383250/could-not-load-assembly-interop-word-version-14-0-0-0