0

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?

wentimo
  • 471
  • 3
  • 12
  • See my answer here: http://stackoverflow.com/a/19955042/2704659 – rory.ap Dec 10 '15 at 18:09
  • There are four versions of Regasm.exe in common use, you have to pick the right one. .NET 2 vs 4 and 32-bit vs 64-bit. Use SysInternals' Process Monitor, you can observe the registry keys being written by Regasm and see Word try to find them back. – Hans Passant Dec 10 '15 at 18:19
  • @Hans Passant I changed to v4 Framework64 and ran the 64 bit version of regasm and now I am getting Run-Time error '-2147024894 (80070002)': Automation error The System cannot find the file specified. Any ideas on how to resolve/troubleshoot? – wentimo Dec 10 '15 at 18:34
  • 1
    Well, you solved that problem. My crystal ball says that you have a dependency on another DLL, Process Monitor can show that too. – Hans Passant Dec 10 '15 at 18:35
  • @Hans Passant I've created filters so I can look at regasm.exe and winword.exe but there are still 1000s of events. Is there any way to further filter down to only relevant info? – wentimo Dec 10 '15 at 19:05
  • It is a character-building utility, work from the bottom of the trace backwards. Can't you tell from your VS solution? If it has more than one project then you'll have this problem. – Hans Passant Dec 10 '15 at 19:12
  • @Hans Passant so I've determined that HKEY_CURRENT_USER\Software\Classes\CLSID doesn't contain any registry keys and running RegAsm.exe in admin dev console is not recreating them. The keys in ROOT seem to exist. I created new guids and still the same issue. What would cause RegAsm to fail to create the CLSID registry keys? – wentimo Dec 10 '15 at 20:53
  • They are not written to HKCU, they go into HKLM. Unclear why you are chasing this, you already solved that problem. – Hans Passant Dec 10 '15 at 20:59
  • I didn't solve anything. Every time I run my vba code it still doesn't work. – wentimo Dec 10 '15 at 21:03

0 Answers0