2

I have the following bit of ruby code which works fine

require 'WIN32OLE'
excel = WIN32OLE.new('Excel.Application')
excel.visible = true
path =  Dir.pwd + '\Testargs.xlsm'
excel.workbooks.open(path)
excel.run('IdontTakeargs')

Where IdontTakeargs is a macro in the Testargs.xlsm workbook.

If I attempt to call a macro which takes arguments ie:

excel.run('Itakeargs(1)')

This code also runs, but for some reason it runs the macro twice. The above snytax is somewhat of a guess as I can't locate an example of calling a macro with arguments.

Any help with what the correct syntax might be or even if what I am attempting to accomplish is even possible (call a macro that accepts arguments from ruby.)

Community
  • 1
  • 1
Pynner
  • 1,007
  • 10
  • 22
  • Could you create a new macro (as simple as possible) with one argument and do excel.run('Mynewmacro(1)')? Does it run twice? – DNNX Dec 21 '11 at 07:54

2 Answers2

1

The syntax to pass an argument to a VBA macro seems to be

excel.run('Itakeargs', '1')

Still, the observed (and reproducable) behaviour is somewhat strange.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
  • Thanks! did you figure that out through experimentation, or documentation? Also as a side note to future vistors multiple arguments take the form (macroname, arg1, arg2, ...) – Pynner Dec 23 '11 at 01:41
  • I googled a bit and found some examples in Python using that syntax. Then, I just tried it. – undur_gongor Dec 23 '11 at 22:14
0

To rule out a silly possibility, you didn't have your new code as

require 'WIN32OLE'
excel = WIN32OLE.new('Excel.Application')
excel.visible = true
path =  Dir.pwd + '\Testargs.xlsm'
excel.workbooks.open(path)
excel.run('IdontTakeargs')
excel.run('Itakeargs(1)')

did you? If you did, that'd explain why two macros were run!

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
  • Thanks for the response. If I run the code as you have listed IdontTakeargs will run once, and Itakeargs(1) will run twice – Pynner Dec 17 '11 at 18:22