4

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
6
  • try something like: Shell("C:\Temp\gc.exe 1", vbNormalFocus) Commented Dec 16, 2015 at 15:23
  • Mmm... I think you can do all from VisualBasic without call a ruby script Commented Dec 16, 2015 at 15:30
  • 1
    I tried Shell(" ruby C:\Folder1\dostuff.rb file1.csv file2.csv" ) Commented Dec 16, 2015 at 15:36
  • @Conny are file1 and file2 in the same directory as your excel sheet? Maybe you should fully qualified name Commented Dec 16, 2015 at 15:38
  • 1
    @Kiril damn you are right, I forgot to add C:\Folder1\ to the files.... Thank you Commented Dec 16, 2015 at 15:40

3 Answers 3

1
+50

The comment section made it clear: fully qualify your parameters, if they are files, and the files are not in the same directory as your Excel sheet.

Shell("ruby C:\Folder\dostuff.rb C:\Folder\file1.csv C:\Folder\file2.csv", vbNormalFocus)
Sign up to request clarification or add additional context in comments.

Comments

1

Sometimes you need to use the environment variable comspec to successfully launch external program from VBA. See these blog search result for examples.

Comments

1

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

Comments

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.