Question posted 2013 · +6 upvotes
I have seen a few posts on this, but I haven’t seen any solutions so far. I have a .jar file that I’m converting to a .NET DLL via IKVM. I’m trying to figure out how to make the methods in the DLL available inside the excel VBA environment. here are the details.
1.) installed IKVM & registered it’s DLL’s to GAC
2.) ran IKVM to create the a .net .dll (mytest.dll)
ikvmc mytest.jar
3.) registered the new .dll
regasm mytest.dll
4.) From here i created a VB.NET project and added mytest.dll and IKVM.OpenJDK.Core.dll as references to the project. I am then able to access the methods within the .dll in .NET. This is great!
5.) what I really want to do is be able to use the .dll in VBA as well. Initially vba wouldn’t accept the .dll directly as it’s a .net library. I attempted to create a type library:
regasm /codebase /tlb mytest.dll
This created a .tlb file which is nice, but it did throw a warning about the library not being strongly named.
6.) then I loaded the .tlb as a reference in my vba editor. This works, however when I try to access the methods nothing shows up. Similarly if I look in the object viewer for my library i can see my two classes but not the members of those classes.
Additionally, I imagine that I probably also need to somehow reference the IKVM.OpenJDK.Core.dll inside VBA as well. However I can’t do that either since it’s a .NET .dll.
Has anyone had success converting a .jar file into something that can be used with VBA?
Accepted answer +10 upvotes
I think you always need to explicitly mark a class to be usable via COM interop. Here’s an example of a Java class that is usable from VBA:
import cli.System.Runtime.InteropServices.*;
@ClassInterfaceAttribute.Annotation(ClassInterfaceType.__Enum.AutoDual)
public class SampleWidget {
public int Add(int x, int y) {
return x + y;
}
}
Here are the steps to compile:
- Copy IKVM.Runtime.dll and all IKVM.OpenJDK.*.dll into the current directory or the GAC.
- Run “ikvmstub mscorlib” to generate mscorlib.jar.
- Create a Java source named SampleWidget.java containing the code above.
- javac -cp mscorlib.jar;. SampleWidget.java
- ikvmc -out:SampleLibrary.dll SampleWidget.class -r:mscorlib.dll
- tlbexp SampleLibrary.dll
- regasm /codebase SampleLibrary.dll (this step needs administrator rights)
Now you can add a reference to the SampleLibrary.tlb from VBA and use the SampleWidget class.
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
.