Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Office 2007 PIA

I built a application, it generates Excel with COM Interop. It works fine on mine and our user's machine: Office 2007, Windows XP SP2. Now I migrate it to another machine, Office 2010, Windows 7 64 bit, and use visual studio 2010 from VS2008, which still works fine. When I deploy it on my user's machine, I got the problem, they use the same Windows 7 64 bit but Office 2007.

So if I install Office 2007 PIA on my Win7 64 bit and Office 2010 machine, change the reference in my visual studio project, will that solve the problem?

like image 287
weslleywang Avatar asked Dec 28 '22 16:12

weslleywang


1 Answers

I know this post was awhile back, but I figured I'd throw in my two cents anyway in case someone stumbles upon it.

A general rule of thumb is to use the PIA version in development that links up with the oldest version of Office you plan to support. So, if you're going to support Office 2007 and up, use version 12 of the PIAs during development.

Add a reference to v12 PIAs (either by downloading the 2007 PIAs online, via VSTO, or from the GAC if you already had Office 2007 instead previously). Now you might think "Ok, I'm referencing v12 in Visual Studio, this should now work on Office 2007". Wrong. Since you're referencing v12 but you have Office 2010 installed, during assembly binding your machine will say "Well, v12 is requested, but we have v14 installed in the GAC and since PIAs are forward compatible, we'll use that". Even though you explicitly tell it to use v12, after completion of a build your machine will be using v14. You can confirm this via FUSLOG. FUSLOG is an excellent tool to use when working with assembly binding. Note: If you're not seeing anything in FUSLOG and you think you should, clear everything in your Internet Explorer history and try again.

To fix this problem you need to stop what's called 'assembly binding redirection'. These PIAs usually have a policy file with them in the GAC. The purpose of the policy file is to redirect older PIA versions to newer versions when resolving the assembly. To see this, do the following:

  1. Press 'Windows key + R' to open the run menu.
  2. Type 'C:\Windows\Assembly\GAC' and press Enter. Note: I am running a 32bit machine. Your PIAs will most likely be installed in a different location in the GAC, such as 'C:\Windows\Assembly\GAC_MSIL'.

Once in the GAC, scroll down and look for the folders of PIAs you reference (e.g. Microsoft.Office.Interop.Excel). If you continue scrolling, you should see folders that start with 'Policy', then a number, then the name of the PIA (e.g. Policy.12.0.Microsoft.Office.Interop.Excel). This is the policy file that performs binding redirection. Open the XML configuration file within that folder and you will see something similar to the following:

<?xml version="1.0" encoding="UTF-16"?><configuration>
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity publicKeyToken="71e9bce111e9429c" name="Microsoft.Office.Interop.Excel" culture="neutral"></assemblyIdentity>
            <bindingRedirect oldVersion="12.0.0.0" newVersion="14.0.0.0"></bindingRedirect>
        </dependentAssembly>
    </assemblyBinding>
</runtime>

In the above code snippet, the tag "bindingRedirect oldVersion ..." is responsible for the PIA assembly redirection. If you comment out that code like the following:

<?xml version="1.0" encoding="UTF-16"?><configuration>
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity publicKeyToken="71e9bce111e9429c" name="Microsoft.Office.Interop.Excel" culture="neutral"></assemblyIdentity>
            <!--<bindingRedirect oldVersion="11.0.0.0" newVersion="12.0.0.0"></bindingRedirect>-->
        </dependentAssembly>
    </assemblyBinding>
</runtime>

Then this redirection will be ignored. I comment it rather then deleting simply because the file does serve a good purpose, just not when developing for older versions of Office. Perform this on each PIA Policy file that you require, and your development machine will stop redirection.

Hopefully this answers your question!

like image 106
StoriKnow Avatar answered Dec 30 '22 09:12

StoriKnow