在同一个解决方案中,是否可以将MS Word加载项中对“ Globals.ThisAddIn”的引用传递给WinForms项目?

问题描述

我有一个Windows窗体应用程序,需要使用Microsoft Word文档,并且在与Word加载项项目相同的解决方案中,无法从WinForms项目中引用“ Globals.ThisAddIn”。当我尝试访问Globals.ThisAddIn时,它引发错误,提示“ Globals.ThisAddIn.get返回null。”

下面的代码引发错误。 “ MyWord”类是MS Word加载项项目中的公共类。从WinForms Project中调用它的方式与Word加载项相同。

using Word = Microsoft.Office.Interop.Word;
using Tools = Microsoft.Office.Tools.Word;
public class MyWord
{

    public MyWord()
    {

    }


    public Tools.Document GetVstoDocument(Word.Document doc)
    {
        Tools.Document ToolDoc = Globals.Factory.GetVstoObject(doc);
        return ToolDoc;

    }

    public Word.Document OpenFromTemplate(string TemplatePath)
    {
        Word.Document doc = Globals.ThisAddIn.Application.Documents.Add(TemplatePath);
        return doc;
    }
}

当前,Word加载项项目设置为与WinForms App同时启动。任何帮助将不胜感激。

解决方法

检查“流程外客户”以及此链接calling-code-in-vsto-add-ins-from-other-office-solutions中的说明和指示

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...