[作者:葛伟华工程师, Office/Project支持团队, 微软亚太区全球技术支持中心]

[English version: http://blogs.technet.com/b/officeasia/archive/2013/01/04/office-2003-vsto-add-in-can-run-in-debug-mode-but-cannot-load-after-installation.aspx]

希望本篇对VSTO的开发人员和Office插件使用者有所帮助。

问题描述:

使用Visual Studio 2008开发的Office 2003插件(下文中与add-in混用),在开发机器上(按F5)调试时可以加载并运行,但是安装到开发机器或者其他机器上无法加载,或者可以加载插件,但是插件的自定义菜单/按钮没有响应。

问题解答:

凡是插件无法加载的问题,都可以按照http://msdn.microsoft.com/en-us/library/ms269003(v=VS.90).aspx 增加环境变量 VSTO_SUPPRESSDISPLAYALERTS=0 和 VSTO_LOGALERTS=1,这样每次启动插件失败都会弹出报错(下图为一个示例)。根据该报错可以做进一步的排错。

 

 

然后我们看一下运行Office2003的VSTO插件需要什么样的前置条件(http://msdn.microsoft.com/en-us/library/bb332051(office.12).aspx):

 

Table 1. Summary of bootstrapper packages that accompany the article

 

Bootstrapper package

Dependent packages

Other conditions

VSTO 2005 SE runtime

The .NET Framework 2.0

 

Visual Studio Tools for Office Language Pack

VSTO 2005 SE runtime

 

Primary interop assemblies redistributable package for the 2003 release of Office

.NET Framework 2.0

Installs all primary interop assemblies if Office 2003 is installed.

Primary interop assemblies redistributable package for the 2007 release of Office

.NET Framework 2.0

Installs all primary interop assemblies if the 2007 release of Office is installed.

 

 

 

当我们用Visual Studio 2005创建Office 2003插件时,这些前置条件可以从插件的安装程序项目属性的前置条件列表里面勾上。但是Visual Studio 2008默认只包含给Office 2007插件使用的前置条件,比如Visual Studio Tools for Office system 3.0 Runtime、Primary Interop Asseblies 2007等,而缺少Office 2003插件需要的前置条件,这是插件加载失败的主要原因。

 

 

 

下面是完整的使用Visual Studio 2008部署Office 2003插件方法:

1. 下载Microsoft Visual Studio 2005 Tools for Office SE Resources Visual Studio 2005 Tools for Office Second Edition Sample: Deploying Office Solutions Using Windows Installer Version 3

2. 访问“C:\Program Files\Microsoft Visual Studio 2005 Tools for Office SE Resources\VSTO2005SE Windows Installer Sample Version 3\packages”,拷贝VSTOSERuntimeOffice2003PIA文件夹到”C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages

3. 下载Download VSTO 2005 SE Runtime http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=2392,拷贝到 “C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\VSTOSERuntime

4.启动Visual Studio 2008命令行,转到“C:\Program Files\Microsoft Visual Studio 2005 Tools for Office SE Resources\VSTO2005SE Windows Installer Sample Version 3\Projects\Checks”

5. 运行下面命令:

cl.exe /Oxs /MT /GS ComponentCheck.cpp advapi32.lib

6.运行成功后, 会看到ComponentCheck.exeComponentCheck.obj

7. 把ComponentCheck.exe复制到“C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\VSTOSERuntime”和C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\Office2003PIA”

8.右键单击setup项目,在Prerequisites里可以勾上:

 

  • .NET Framework 2.0
  • Windows Installer 3.1
  • Microsoft Office 2003 Primary Interop Assemblies
  • Microsoft Visual Studio 2005 Tools for Office Second Edition runtime

9. 按照http://msdn.microsoft.com/en-us/library/bb332052(v=office.12).aspx#officevstowindowsinstallerwalkthrough_creatinginstallerpackagestodeploysolutions ,从Adding a Custom Action to Grant Trust to the Assembly一节开始往下做,直到To add a launch condition for the VSTO 2005 SE runtime

 

     1)      安装插件后,插件的功能如果需要使用,必须用.NET Framework 2.0带的CASPOL.EXE给插件的所有文件加上信任级别,否则会出现插件菜单无法响应的问题。加载”C:\Program Files\Microsoft Visual Studio 2005 Tools for Office SE Resources\VSTO2005SE Windows Installer Sample Version 3\projects\SetSecurity”项目

修改SetSecurity项目里的CaspolSecurityPolicyCreator.cs

 

为了简化部署步骤(不使用强签名),用下面代码覆盖原来的部分(修改的部分用高亮显示了):

 

  

 

           // Add the solution code group. Grant no permission at this level.

 

            string arguments = policyLevel + " -q -ag " + parentCodeGroup + " -url \"" + solutionInstallationUrl + "\" FullTrust -n \"" + solutionCodeGroupName + "\" -d \"" + solutionCodeGroupDescription + "\"";

 

            try

 

            {

 

                RunCaspolCommand(frameworkFolder, arguments);

 

            }

 

            catch (Exception ex)

 

            {

 

                string error = String.Format("Cannot create the security code group '{0}'.", solutionCodeGroupName);

 

                throw new Exception(error, ex);

 

            }

 

 

 

            //// Add the assembly code group. Grant FullTrust permissions to the main assembly.

 

            //try

 

            //{

 

            //    // Use the assembly strong name as the membership condition.

 

            //    // Ensure that the assembly is strong-named to give it full trust.

 

            //    AssemblyName assemblyName = Assembly.LoadFile(assemblyPath).GetName();

 

            //    arguments = policyLevel + " -q -ag \"" + solutionCodeGroupName + "\" -strong -file \"" + assemblyPath + "\" \"" + assemblyName.Name + "\" \"" + assemblyName.Version.ToString(4) + "\" FullTrust -n \"" + assemblyCodeGroupName + "\" -d \"" + assemblyCodeGroupDescription + "\"";

 

 

 

            //    RunCaspolCommand(frameworkFolder, arguments);

 

            //}

 

            //catch (Exception ex)

 

            //{

 

            //    try

 

            //    {

 

            //        // Clean the solutionCodeGroupName.

 

            //        RemoveSecurityPolicy(machinePolicyLevel, solutionCodeGroupName);

 

            //    }

 

            //    catch { }

 

 

 

            //    string error = String.Format("Cannot create the security code group '{0}'.", assemblyCodeGroupName);

 

            //    throw new Exception(error, ex);

 

            //}

Custom Action中增加Primary output from SetSecurity

    3) Install动作的CustomActionData示例如下:

/assemblyName="<add-in name>.dll" /targetDir="[TARGETDIR]\" /solutionCodeGroupName="<corp name>.<add-in name>.dll" /solutionCodeGroupDescription="Code group for <add-in name>" /assemblyCodeGroupName="<add-in name>" /assemblyCodeGroupDescription="Code group for <add-in name>" /allUsers=[ALLUSERS]

   4) Uninstall和Rollback动作的CustomActionData示例如下:

/solutionCodeGroupName="<corp name>.<add-in name>.dll"

    5) 增加Launch Conditions

10. 重新编译插件项目和安装程序项目,然后将插件安装到目标机器。这个插件是可以在Office 2003和2007上使用的。

衍生问题:

如果上述问题中的项目(Visual Studio 2008 + Office 2003机器上开发的插件项目)在Visual Studio 2008 + Office 2007机器上打开,会收到升级提示。升级之后,项目中所有对于Office 2003的VSTO runtime和PIA的引用都会升级为Office 2007的。如果直接编译安装程序项目然后安装插件,插件依旧无法使用。

产生这个问题的原因是Office 2007的插件在注册表中的Manifest键值规范是[TARGETDIR]<add-in name>.vsto|vstolocal,而Office 2003插件的Manifest键值规范是[TARGETDIR]<add-in name>.dll.manifest。解决该问题有两个办法:

1. 新建一个Office 2007的插件项目,然后把原来的代码复制过去

2. 检查安装程序项目中是否包含<add-in name>.dll.manifest和<add-in name>.vsto,可以右键单击安装程序->Add->File,把这两个文件加上

 

再右键单击安装程序->View->Registry,找到Manifest,原值是[TARGETDIR]<add-in name>.dll.manifest,修改成[TARGETDIR]<add-in name>.vsto|vstolocal

 

 

重新发布项目后,在Office 2007中插件可以加载了。但是这个插件无法在Office 2003上使用。

 

类似问题记录在:

 

http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/75dbff40-181b-434f-87fe-7fa7e21a5828

相关资料:

Deploying Visual Studio 2005 Tools for the Office System SE Solutions Using Windows Installer (Part 1 of 2)

http://msdn.microsoft.com/en-us/library/bb332051(office.12).aspx

 

Deploying Visual Studio 2005 Tools for the Office System SE Solutions Using Windows Installer: Walkthroughs (Part 2 of 2)

http://msdn.microsoft.com/en-us/library/bb332052(v=office.12).aspx

 

Troubleshooting COM Add-In load failures

http://blogs.msdn.com/b/vsod/archive/2008/04/22/troubleshooting-com-add-in-load-failures.aspx

 

VSOD (CSS) team blog:

Visual Studio Office Development (VSOD) Support Team

http://blogs.msdn.com/b/vsod/

 

VSTO (Product team) team blog:

Office Development with Visual Studio

http://blogs.msdn.com/b/vsto/