Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Excel Macro to run Java program

I have learnt to read and write an Excel file using a Java program with the help of Jxl and POI API. Is it possible to run a Java program with the help of macros?

like image 560
Nimit_ZZ Avatar asked Jul 05 '12 11:07

Nimit_ZZ


1 Answers

i've used it.. Attention, use javaw otherwise a black window is popping up

Dim result As String
Dim commandstr As String

commandstr = "javaw -jar somejar someparameter"


' try with or without cast to string
result = CStr( shellRun(commandstr) )


'somewhere from SO but forget.. sorry for missing credits

Public Function ShellRun(sCmd As String) As String

    'Run a shell command, returning the output as a string'

    Dim oShell As Object
    Set oShell = CreateObject("WScript.Shell")

    'run command'
    Dim oExec As Object
    Dim oOutput As Object
    Set oExec = oShell.exec(sCmd)
    Set oOutput = oExec.StdOut

    'handle the results as they are written to and read from the StdOut object'
    Dim s As String
    Dim sLine As String
    While Not oOutput.AtEndOfStream
        sLine = oOutput.ReadLine
        If sLine <> "" Then s = s & sLine & vbCrLf
    Wend

    ShellRun = s

End Function
like image 87
mschwehl Avatar answered Sep 17 '22 08:09

mschwehl