Search code examples
c#excelcomtlbexpvba

Resolve .net tlb reference



Right now I am writing a .net dll which should be useable within VBA code (Excel, Access etc.). The following setup is working fine:

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
[Guid("8079e4a4-1e4b-4788-92ba-9d5b017fa9be")]  //Allocate your own GUID
public interface ICommunication
{
    string TestProp { get; set; }
    string Login();
    string Disconnect();
}

[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[Guid("19fd86e2-f1b9-478c-ba7a-bd76bdf19b85")]  //Allocate your own GUID
[ProgId("TestDll.Communication")]
public class Communication : ICommunication
{
    public string TestProp { get; set; }

    public string Login()
    {
        // use of BouncyCastle here
        return "logged in";
    }

    public string Disconnect()
    {
        // ...
        return "disconnected";
    }
}

By referencing the generated tlb file I can properly use the Property aswell Disconnect function however calling the Login function leads to problems ("File not found" messagebox in Excel) which I guess are related to the usage of referenced BouncyCastle.

Sub Button1_Click()
    Dim o: Set o = CreateObject("TestDll.Communication")
    Dim value As String
    value = o.Login()
    MsgBox value
End Sub

What is the best way to deal with references to other .net assemblies inside com visible libraries? So far I tried registering bouncycastle to the GAC with no success.

Thanks :)


Solution

  • It was indeed like suggested above a file which could not be read: On each build I place a .txt in the bin directory of the project which is read runtime. Using the dll within my solution the file could be found since the relative path was my bin directory however using the tlb the relative root path was the document folder of the signed in windows user.

    Funny how I thought the whole time that the error was related to the way I setup my dll as com visible :).