0

Currently I am following this procedure:

  1. Create some C# class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace TestAgain
{
    public class Test
    {
        void testIt() {
            MessageBox.Show("Yellow world");
        }
    }
}

  1. Sign the project.
  2. In AssemblyInfo.cs set [assembly: ComVisible(false)] to [assembly: ComVisible(true)].
  3. Build the project.
  4. Run regasm

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe" -tlb -codebase "C:\Users\sancarn\Desktop\tbd\TestAgain\TestAgain\bin\Debug\TestAgain.dll" -verbose

The output from regasm is as follows:

Microsoft .NET Framework Assembly Registration Utility version 4.7.3056.0
for Microsoft .NET Framework version 4.7.3056.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Types registered successfully
Type 'TestAgain.Test' exported.
Assembly exported to 'C:\Users\sancarn\Desktop\tbd\TestAgain\TestAgain\bin\Debug\TestAgain.tlb', and the type library was registered successfully

So the type TestAgain.Test is exported and the type library has been registered. Great!

So I go to Excel-VBA and test it with:

Sub testIt()
    Dim o As Object
    Set o = CreateObject("TestAgain.Test")
    Call o.testIt
End Sub

And get the error Error 429: ActiveX component can't create object. I.E. It's not registered?

Has anyone got any ideas as to why this isn't working?


Note: I have managed to get a Com-Visible application before with interfaces but I've been looking for a cleaner method and a user told me regasm could do the job.

Sancarn
  • 2,575
  • 20
  • 45
  • Well, making it so much harder to write correct VBA code did not make it cleaner. The CreateObject argument wrong, it should be "ProjectName.Test". Where ProjectName is the name of your C# project, the namespace plays no role. You could also be using the wrong Regasm.exe version (32-bit vs 64-bit) but unlikely to get that wrong if you had the earlier version working correctly. – Hans Passant Aug 07 '18 at 10:19
  • @HansPassant "making it so much harder to write correct VBA code did not make it cleaner" huh...? How is this making it harder to write VBA code...? The project name is `TestAgain` so that wouldn't be the issue. Sadly I've ran regasm on both 32 and 64 bit, but to no avail. – Sancarn Aug 07 '18 at 11:24

1 Answers1

0

I already knew this but it slipped my mind:

  • Only public methods are exposed to COM.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace TestAgain
{
    public class Test
    {
        public void testIt() {
            MessageBox.Show("Yellow world");
        }
    }
}

This works like a charm.

Sancarn
  • 2,575
  • 20
  • 45