Different Interop references on two different computers doesn't work

前端 未结 3 1822
终归单人心
终归单人心 2020-12-31 10:58

When I add a reference to Microsoft.Office.Interop.Excel on my computer, Visual Studio adds this to the project file:



        
相关标签:
3条回答
  • 2020-12-31 11:26

    I don't see a problem with your approach either.

    Typically VS will generate an interop assembly for COM components automatically when you add a reference to the component. However, when you add a reference to one of the Office components (XP or any later version), a reference to the pregenerated (and optimized) primary interop assembly from Microsoft is added as in your first example. The line

    <WrapperTool>primary</WrapperTool>
    

    means that this PIA is used.

    If you correctly added the PIA reference the CopyLocal property of this reference should be set to false and the Path property should be something like

    C:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Excel\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll
    

    You will find some more details on interop assemblies in this MSDN article.

    To get the first method working it is necessary that the Office Primary Interop Assemblies (PIAs) are installed on the machine. There is a redistributable version available from Microsoft:

    • Office 2003 PIAs
    • Office 2007 PIAs

    AFAIK, these PIAs only get installed by the Office setup when the .NET Framework has already been installed, that's why there is a separate redistributable for the PIAs.

    Note: Make sure that you reference the version of Office that you are targeting. When targeting several versions of Office, you might get some problems however. A solution in that case might be late binding (if performance is not an issue).

    0 讨论(0)
  • 2020-12-31 11:37

    I found the cleanest way to use it, this also allows for multiple versions of the interop, is to create a shared bin\Office Interop\11 or 12\Microsoft.Office.Interop.Excel.dll\ and refeferenced them from the project, works a treat for different version

    0 讨论(0)
  • 2020-12-31 11:38

    I've used Excel automation way more than I would like to admitt, and I have never referenced Interop.Excel.dll. I've always referenced the former. Why is he referencing that, and what errors does he get?

    Are you guys referencing the same version of excel (5.0 verses 11.0)? Do you guys have the exact same version of office, service pakcs and all? This could be the differance.

    0 讨论(0)
提交回复
热议问题