Unit testing Excel addin using VSTO

2

I've been trying to test my addin without success, and even after looking at various sources like MSDN and StackOverflow

I can't seem to be able to do it. Here's my current code (I trimmed to only have the relevant parts)

AssemblyInfo.cs

[assembly: ComVisible(true)]
[assembly: InternalsVisibleTo("MyAddInTest")]

ThisAddIn.cs

private AddinHelper _comAddinObject;
protected override object RequestComAddInAutomationService()
{
    return _comAddinObject ?? (_comAddinObject = new AddinHelper());
}

AddinHelper.cs

[ComVisible(true)]
[Guid("B523844E-1A41-4118-A0F0-FDFA7BCD77C9")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IAddinHelper
{
    string GetString();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("A523844E-1A41-4118-A0F0-FDFA7BCD77C9")]
[ComSourceInterfaces(typeof(IAddinHelper))]
public class AddinHelper : StandardOleMarshalObject, IAddinHelper
{
    public string GetString()
    {
        return Globals.ThisAddIn.Application.ActiveWorkbook.Name;
    }
}

ThisAddInTest.cs

Application = new Application();
Workbook = Application.Workbooks.Add();
COMAddIns comAddins = Application.COMAddIns;
COMAddIn comAddin = comAddins.Item("MyAddIn");
IAddinHelper comObject = (IAddinHelper) comAddin.Object;

The main project references the .NET Assembly version of Microsoft.Office.Interop.Excel with Embed Interop Types set to true. My test project references my VSTO project, and the COM (ActiveX) version of the Interop.

The exception I get is:

Unable to cast COM object of type 'System.__ComObject' to interface type 'MyAddIn.IAddinHelper'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B523844E-1A41-4118-A0F0-FDFA7BCD77C9}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

I currently use Office 2013 (so Office 15.0) and the references are all pointing to that version, .NET 4.5.3 and Visual Stuio Community 2017. The SO link that I provided seems to be the exact same issue, however I still have the exception when running my tests unlike there.

c#
excel
testing
vsto
office-interop

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0