I have upgraded from Windows 7/Visual Studio 2015 to Windows10/Visual Studio 2019. I wish to create a C# COM object which I can call from Excel(365) VBA code.
Using Win 7/Visual Studio 2015 the following very simple code compiles as a C# class library and creates a COM object that I can call successfully from Excel 2013 VBA:-
using System.Runtime.InteropServices;
namespace TestLib {
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class C_Hello {
public string Hello() {
return "Hello";
}
}
}
In the Assembly Info I have ticked the checkbox "Make the Assembly COM Visible" and in the Build info I have ticked the checkbox "Register for COM interop"
The Excel 2013 VBA code is equally simple:
Sub Test()
Dim x As TestLib.C_Hello
Set x = New TestLib.C_Hello
ActiveSheet.Range("C3").Value = x.Hello
End Sub
where TestLib is the C# COM module created in Visual Studio.
In the Win 7/VS2015/Excel 2013 environment everything works fine, but under after transferring and compiling under Win 10/ VS2019 the Excel 365 VBA code errors at the line:-
Set x = New TestLib.C_Hello
with the error message
Run Time error '-2147221164 (80040154)'
Class not registered
Can anyone tell me what I have to do to get this working in the Windows 10 / VS2019 / Excel 365 environment.? Is it Windows 10 or Visual Studio 2019 that is doing things differently?