且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过 RequestComAddInAutomationService 在 C# .NET 中的 VSTO 单元测试 Office 插件

更新时间:2023-12-05 22:36:46

Solution

The Project with methods to be Tested has to use the PIA Microsoft.Office.Interop.PowerPoint via the .Net reference tab.

In the Unit Test Project you have to use the Microsoft Powerpoint 1X.0 Object Library via the COM reference tab - its an ActiveX.

The confusing thing is in Solution Explorer they are both called: Microsoft.Office.Interop.Powerpoint


When you specify the correct References you'll see this error on the line where the exception occurs:

Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

To solve that simply add a .Net reference to the Microsoft.CSharp.dll in the Unit Test project.


Next we will run into the error you're seeing.

Firstly add a unique GUID to the Interface & class (this will overcome the error):

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

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("A523844E-1A41-4118-A0F0-FDFA7BCD77C9")]
[ComSourceInterfaces(typeof(IAddinHelper))]
public class AddinHelper : StandardOleMarshalObject, IAddinHelper

Secondly temporarily make the private AddinHelper _comAddinObject; public in Scope (you can do your [assembly: InternalsVisibleTo("MyProject.Tests")] later when its working).

Thirdly, check that Powerpoint has not disabled the COM add-in. This sometimes happens silently, without the Office App complaining.

Lastly, make sure Register for COM is ticked.

Viola:


Ref: I pulled my hair out working this out years ago helping this fellow SO'r: Moq & Interop Types: works in VS2012, fails in VS2010?