1

I looked at other posts and couldn't find the solution.

I am trying to use C# dll I created in VBA code without having to add reference.

In my VBA code, I declared:

Public Declare Function message Lib "path_to_my_dll" _
 (ByVal message As String) As String


Sub Test()

Dim hello As String

    hello = message("hi!!")
    Debug.Print hello

End Sub

I get an error saying entry point for my dll couldn't be found.

The C# Code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace DLLImport
{
    public class Class1
    {
        [DllImport("DLLImport", EntryPoint = "Run")]
        extern string Run(string message)
        {
            return message;
        }
    }
}

Thank you in advance for your help!!

3
  • You have to use Interface to be able to use *.dll in VBA. I'll find my past answer and post a link to it. ===EDIT=== Got it! Link Commented Feb 7, 2019 at 21:09
  • 2
    This is a DllExport, not a DllImport. VBA will also only use the StdCall calling convention. I've run across claims that this is possible to do from an unmanaged c++ caller, but have never attempted it. Commented Feb 7, 2019 at 21:22
  • No Dll-Export-Attribute is available with c# yet, but you can try this Nuget package. Commented Feb 7, 2019 at 22:42

1 Answer 1

0

You might want to use InteropServices to make a COM Visible DLL

using System.Runtime.InteropServices;   

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("your-GUID-1")]
public interface _Visible_Methods
{
    //--------< _Visible_Methods >--------

    //*visible COM Methods of this Control under Office,Excel, Word

    string get_Hello();

    //--------</ _Visible_Methods >--------
}

Source: https://codedocu.com/Net-Framework/Controls/COM-ActiveX/Create-C_hash_-COM-Control-for-Office?2382

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Thank you for the help. But this method still requires adding reference in the VBA editor. I am trying to import DLL directly from the absolute path in local drive and call the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.