Is there a way to start a Ruby script from through an Excel macro?
The macro creates two CSV files and I would like to automate the procss of running
ruby dostuff.rb file1.csv file2.csv
Sometimes you need to use the environment variable comspec to successfully launch external program from VBA. See these blog search result for examples.
There are multiple ways to run a ruby script with VBA.
With Shell, asynchronous with console:
Public Sub RunRuby(file As String, ParamArray args())
Shell "ruby.exe """ & file & """ " & Join(args, " ")
End Sub
With Shell, synchronous without console:
Public Sub RunRuby(file As String, ParamArray args())
Shell "rubyw.exe """ & file & """ " & Join(args, " ")
End Function
With WScript.Shell, synchronous without console and with exit code:
Public Function RunRuby(file As String, ParamArray args()) As Long
Dim obj As Object
Set obj = CreateObject("WScript.Shell")
RunRuby = obj.Run("rubyw.exe """ & file & """ " & Join(args, " "), 0, True)
End Function
And to set the working directory in case there's a relative path:
ChDir "C:\Folder1"
, or with the folder of the current workbook:
ChDir ThisWorkbook.Path
Shell("C:\Temp\gc.exe 1", vbNormalFocus)Shell(" ruby C:\Folder1\dostuff.rb file1.csv file2.csv" )