I need to get the output string of a java function into my VBA project. As a fast example, I am trying to get the output of the java version installed but in the real application it will be other private functions.
First attempt:
' Needs a reference to Windows Script Host Object Model
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub get_java_output()
Dim cmd_windows As New WshShell
Dim execution_cmd As WshExec
Dim command_str As String
command_str = "java -version"
Set execution_cmd = cmd_windows.exec("cmd.exe /c " & command_str)
Do While execution_cmd.Status = WshRunning
Sleep 20
Loop
final_string = execution_cmd.StdOut.ReadAll
Debug.Print final_string
End Sub
Second attempt:
Sub get_java_output_2()
Dim windows_shell As Object
Set windows_shell = CreateObject("WScript.Shell")
command_str = "java -version"
shell_output = windows_shell.Run("cmd /c " & command_str & " > c:\temp\output.txt", 0, False)
Set fso = CreateObject("Scripting.FileSystemObject")
Set File = fso.OpenTextFile("c:\temp\output.txt", 1)
final_string = File.ReadAll
File.Close
Debug.Print final_string
End Sub
None of them worked for me.
I would like to avoid the use of temporary files as in my second attempt example. In the final usage, I will call this function hundred of thousand of times, and I prefer not to create that amount of files or edit that file so many times...