-1

I need to add new functionality to vba in access. I already tried a lot of tutorials: http://www.office-loesung.de/ftopic268792_0_0_asc.php (german) and http://www.codeproject.com/Articles/555660/Extend-your-VBA-code-with-Csharp-VB-NET-or-Cpluspl (cant post more cause of rep)

but none of them works for me. The customer has office 2013 and i am using .net 4.5.

We want to provide the customer a rest api which is already written in c#/.net and which needs some encryption dlls of the .net world.

I tried to create an dll with "Interop COM" switch on and could add a reference of the dll to my VBA test Sheet BUT neither objects nor static test functions have been working. There even is no intelisense in VBA. A restart of the program did not solve the problem either. (though somehting went wrong before)

The error message i retrieve is "Runtime Error 429 - ActiveX Component Can't Create Object" (for my own code as well as for the code from codeplex (with all modules, C#, VBA, C++/Cli))

Is there any other way to go, adding new functionality to VBA / extending it with own functions?

1
  • What specific functionality are you trying to add? I realize yours is a more general question, but if you could detail specifically what you are trying to do then maybe there is a "stop-gap" that could be done. Commented Jan 13, 2016 at 20:52

1 Answer 1

-1

Okay so I finally found a way myself.

In this post calling managed c# functions from unmanaged c++ there is the NuGet Package Unmanaged Exports linked. So I created a new C# DLL Project, inserted for testing purpose following code:

using RGiesecke.DllExport;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
    internal static class Class1
    {
        [DllExport("square", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
        static Double Square(Double dateValue)
        {
            return dateValue * dateValue;
        }

        [DllExport("test", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
        static String Test()
        {
            return " ok";
        }

        [DllExport("mytest2", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
        static void Test4(String doSomething)
        {
            // Manipulate (and obisously create new) String
            doSomething += " ok";
        }
    }
}

Compiled this with x64 switch on, and created an VBA Sheet in excel with:

Option Explicit

Private Declare PtrSafe Function square Lib "C:\Users\<Name>\documents\visual studio 2013\Projects\ClassLibrary1\ClassLibrary1\bin\x64\Release\ClassLibrary1.dll" (ByVal dateValue As Double) As Double
Private Declare PtrSafe Function test Lib "C:\Users\<Name>\documents\visual studio 2013\Projects\ClassLibrary1\ClassLibrary1\bin\x64\Release\ClassLibrary1.dll" () As String
Private Declare PtrSafe Function mytest2 Lib "C:\Users\<Name>\documents\visual studio 2013\Projects\ClassLibrary1\ClassLibrary1\bin\x64\Release\ClassLibrary1.dll" (ByRef dateValue As String)

Private Sub CommandButton1_Click()
    Dim zahl As Double
    zahl = CInt(TextBox1.text)
    zahl = square(zahl)
    Label1.Caption = CStr(zahl)

    Dim text As String
    text = "This is"
    mytest2 (text)
    MsgBox text
End Sub

The only error I still get is when calling the test function. Returning an string seems to break excel. (Program instantly quits) But I think that should be solved in another question.

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

Comments

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.