VSTO按需加载

时间:2018-03-02 15:23:08

标签: ms-word ms-office vsto office-addins

我有一个Word 2016 VSTO,我使用VS2015开发并使用InstallShield部署到Windows 10计算机。如果我将LoadBehavior设置为3,它会加载并正常工作。

我需要的是按需加载。我试过将LoadBehavior设置为9但没有骰子。创建Word文档(最终加载VSTO)的Web应用程序最初是为Word 2010编写的,并且使用添加的GUID(C:\ myAddIn.vsto | GUID | vstolocal)将VSTO路径嵌入到Word文档中。在VSTO尝试加载时,打开Word 2016中的Web应用程序创建的文档会引发此错误:

  

Microsoft.VisualStudio.Tools.Applications.Runtime.CannotCreateStartupObjectException:   无法创建启动对象myAddin.ThisAddIn

的实例

我在VS2013 / 2015上看到的任何教程都没有引用在VSTO路径中嵌入GUID;只需将LoadBehavior设置为9。

文件中没有列出发布商 - >选项 - > AddIns。 VSTO列在非活动AddIns下,我希望在按需加载之前。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

如果您希望代码仅针对某些文档执行,例如您描述的(来自评论)

  

我只希望为Web创建的Word文档加载AddIn   应用。从开始菜单打开Word不应加载   外接程序。

然后最好使用文档级自定义,而不是加载项。

附加到文档级自定义的代码将加载文档,并在文档关闭时卸载。可以创建文档级自定义并分发文档,或者稍后使用VSTO的ServerDocument类附加代码。

因为在您的情况下,文档是由Web应用程序生成的,所以指示使用ServerDocument。

以下是MSDN文章的主要内容:

=============================================

将托管代码扩展附加到文档

  1. 在不需要Microsoft Office的项目中,例如控制台应用程序或Windows窗体项目,添加对 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dllMicrosoft.VisualStudio.Tools.Applications.Runtime.dll assemblies

  2. 将以下Imports或using语句添加到代码文件的顶部。

    using Microsoft.VisualStudio.Tools.Applications; using Microsoft.VisualStudio.Tools.Applications.Runtime;

  3. 调用静态AddCustomization方法。

  4. 以下代码示例使用AddCustomization重载。此重载采用文档的完整路径和Uri,该Uri指定要附加到文档的自定义的部署清单的位置。此示例假定桌面上名为WordDocument1.docx的Word文档,并且部署清单位于桌面上名为Publish的文件夹中。

    string documentPath = System.Environment.GetFolderPath(
        Environment.SpecialFolder.Desktop) + @"\WordDocument1.docx";
    int runtimeVersion = 0;
    
    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
    
        // Make sure that this document does not yet have any Visual Studio Tools 
        // for Office customizations.
        if (runtimeVersion == 0)
        {
            string deployManifestPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.Desktop) + @"\Publish\WordDocument1.vsto";
    
            Uri deploymentManifestUri = new Uri(deployManifestPath);
            ServerDocument.AddCustomization(documentPath, deploymentManifestUri);
            System.Windows.Forms.MessageBox.Show("The document was successfully customized.");
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("The document is already customized.");
        }
    }
    catch (FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (DocumentNotCustomizedException ex)
    {
        System.Windows.Forms.MessageBox.Show("The document could not be customized.\n" +
            ex.Message);
    }
    
    1. 构建项目并在要附加自定义的计算机上运行该应用程序。计算机必须安装Visual Studio 2010 Tools for Office Runtime。

答案 1 :(得分:0)

您是否尝试将LoadBehaviour设置为0x10 => Load first time, then load on demand?这应该使Office在您第一次执行它时加载您的插件并在内部缓存它。然后它会将值更改为0x9 ...随后的时间,应用程序将按需加载。