I'm attempting to create a C# DLL to be called in VBA for a Word Document.
Here is the source of the DLL:
using System;
using System.Runtime.InteropServices;
namespace VBAtoCsharpExample
{
[Guid("0F2E10D5-987B-4D86-BC8B-14FFC280EC13")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface IComCalculator
{
[DispId(1)]
int Add(int number1, int number2);
}
[Guid("D68DEB4E-A53A-4FED-BB60-BC56384B7812")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[ProgId("VBAtoCSharpExample.Calculator")]
public class Calculator : IComCalculator
{
public int Add(int number1, int number2)
{
return number1 + number2;
}
}
}
I am running VS2015 Community as Administrator. I set 'Make Assembly COM-Visible' in Assembly Settings and set Register for COM interop in the Build tab. After that I ran RegAsm.exe with the \codebase and \tbl tags. Regasm ran successfully.
I created a new word document, added a reference to VBAtoCSharpExample and wrote this module
Sub Main()
Dim x As Integer
Dim objCalculator As VBAtoCSharpExample.Calculator
Set objCalculator = New VBAtoCSharpExample.Calculator
''Set objCalculator = CreateObject("VBAtoCSharpExample.Calculator")
x = objCalculator.Add(1, 2)
ActiveDocument.Range.Text = CStr(x)
End Sub
When I run this module I get "Run-time error '429': ActiveX component can't create object" on this line:
Set objCalculator = New VBAtoCSharpExample.Calculator
This also happens on the other line that's commented out. My google-fu brought me to this point but I'm coming up short as to why this is happening. Are there any steps I missed or mistakes in the code that may be causing this problem?