Search code examples
excelvbaf#com

How to create a COM visible dll in F#


My aim is to create a COM Visible Type that can be imported into VBA (Excel) and consumed there.

The Excel Object Browser can see my class but not any public methods of the class and fails on the attempt at calling a public method.

My F# Code:

namespace DotNetLibrary
type public Class1() = 
    member public  this.DotNetMethod (x:string) = "Hello" + x

In the AssemblyInfo.fs I also amend to [<assembly: ComVisible(true)>]

I run regasm with switches of /codebase /tlb and a .tlb file is generated.

VBA finds my library in the references browser but does not pick up the DotNetMethod defined on Class1 above.

I have tried to follow the C# guidance for this topic in getting to where I am at the moment but I'm not getting to the finish line.


Solution

  • Run-Time Errors

    The reason that VBA could not run the method succesfully is because I used the 32-bit version of the regasm utility instead of the 64-bit version to register the type library for COM. Given that I am using 64-bit Excel it needed 64-bit COM type libraries.

    Compile-Time Issues

    The reason that VBA could not identify the methods in the IDE could be related to the above, but is also likely to be due to the interface issues raised by @HansPassant in his comments above.