我有一个Excel加载项wirtten由我。现在我想要一个Word加载项,它从一个接口公开的Excel加载项中调用一些函数:
在Excel中公开的类:
[ComVisible(true)]
public interface IWordInterface
{
string WordTest();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class WordInterface : IWordInterface
{
public string WordTest()
{
return "Hello from ExcelToMath";
}
}
在主要的Excel插件类中:
protected override object RequestComAddInAutomationService()
{
if (Word == null)
Word = new Excel_Integration.WordInterface();
return Word;
}
现在我尝试在Word加载项中调用它:
Excel.Workbook Wb=(Excel.Workbook)Marshal.BindToMoniker("E:\\Test.xlsx");
Office.COMAddIns AddIns = Wb.Application.COMAddIns;
object addInName = "ExcelToMath";
Office.COMAddIn addIn = Wb.Application.COMAddIns.Item(ref addInName);
IWordInterface utilities = (IWordInterface)addIn.Object;
最后一行:
IWordInterface utilities = (IWordInterface)addIn.Object;
抛出异常
HRESULT:0x80004002(E_NOINTERFACE)
从excel中调用加载了类似代码时,它没有问题:
Office.COMAddIns AddIns = Globals.ThisAddIn.Application.COMAddIns;
object addInName = "ExcelToMath";
Office.COMAddIn addIn = Globals.ThisAddIn.Application.COMAddIns.Item(ref addInName);
Excel_Integration.IWordInterface utilities = (Excel_Integration.IWordInterface)addIn.Object;
string s=utilities.WordTest();
BindToMoniker调用是成功的,因为我可以使用VSTO从Word更改Excel Workbook中的数据。
我会很感激任何建议。