1

The question is how to return a string from java method in VBA. For example I have a code in java:

package someclass;

public class SomeClass {

    public static String someMethod(String s){
        return "this is some string: "+s;
    }

    public static void main(String[] args) {
        System.out.println(someMethod("Hello world!"));
    } 

}

Now the question is how to call someMethod in VBA?

8
  • Not sure what you're asking, do you want to convert this java code to VBA? Commented Dec 18, 2017 at 23:21
  • The only way I see is you make a DLL (using C++, VB.NET or similar others) that consumes your Java functions, then add a reference to that DLL to your VBA project and call them via the DLL which serves as bridge. As far as I know, there's no direct bridge between VBA and Java. You might run a method from VBA of your JAR file using a shell command, but I don't think you can return the function in VBA though. Commented Dec 18, 2017 at 23:28
  • This is just example. Off course there is no sense to use particular example in real VBA. I had encryptor and decryptor methods in java, so I want to use this methods in VBA. Commented Dec 18, 2017 at 23:33
  • stackoverflow.com/questions/11343769/… Commented Dec 19, 2017 at 0:56
  • I tried to use ikmvc (weblog.ikvm.net/2015/08/26/IKVMNET81ReleaseCandidate0.aspx) to convert jar to dll, but when I registered it with regsvr32 I got the error message: The module "<mypath>\mydll.dll" was loaded but the entry-point DllRegisterServer was not found. Commented Dec 19, 2017 at 11:37

2 Answers 2

1

Use Jinx, the Excel Java Add-In, to call your Java method as an Excel macro.

See https://exceljava.com for details.

All you would need to do in your example is to add the @ExcelMacro annotation to your method to make it callable from Excel as a macro.

package someclass;

import com.exceljava.jinx.ExcelMacro;

public class SomeClass {

    @ExcelMacro
    public static String someMethod(String s){
        return "this is some string: "+s;
    }

    public static void main(String[] args) {
        System.out.println(someMethod("Hello world!"));
    } 

}

See https://exceljava.com/docs/macros.html for more details about writing Excel macros in Java using Jinx.

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

Comments

0

Thanks to @mschwehl answer (i.e. possible solution) here:
stackoverflow.com/questions/11343769/

2 Comments

Close as duplicate if it is the same.. please dont copy paste entire answer
Hi @SurajRao, Sorry I didn't know It's forbidden.

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.