This may be of limited value to many folks here, but I thought it kinda
neat.
I’m writing about Ruby, using Word for Windows, and find myself keeping a
GVim session running where I can test code before adding to the document.
I wondered if there was a way to get Word to evaluate Ruby text right from
the document itself.
I came up with a Word macro that uses gvim. There may be better ways,
perhaps sending it directly to the Ruby interpreter, but this approach has
the advantage that, after the code is run, it’s sitting in a GVim session
in case you want to edit it. (It requires you have gvim installed; you’ll
have to adjust the path to gvim.)
Sub EvalRuby()
’ Takes selection and runs it through ruby interpreter via Vim
’ First, make sure the selection is in the clipboard buffer …
Selection.Copy
’ Launch gvim, the graphical version of vim …
ret = Shell("F:\vim\vim60\gvim.exe ", vbNormalFocus)
’ Set focus to the launched app …
AppActivate ret
’ Paste the copied text. First, put Vim into insert mode:
SendKeys “{ESC} i”
’ Now insert the text with CTRL-V. It looks odd here because of the
’ SendKeys syntax for special control keys …
SendKeys “^(v)”
’ Save the file someplace in preparation for executing it …
SendKeys “{ESC}:w! c:\temp\word.rb {ENTER}”
’ Now tell Vim to execute the file using ruby
SendKeys “{ESC}:!ruby17 {%} {ENTER}”
End Sub
ruby17 is actually a DOS batch file I use to run ruby 1.7 (If i just call
’ruby’ it calls the PragProg 1.6 version.)
I’ve mapped this macro to a Word keyboard command, so now I can write some
code, select the text, and hit CTRL+SHIFT+R to see it run.
(I also believe there should be a way to use ActveRubyScript as a scripting
language in a VBA macro, but I haven’t pursued it. )
James