[#40130] Ruby/Tk の講習会 — Hidetoshi NAGAI <nagai@...>

永井@知能.九工大です.

12 messages 2004/10/25

[ruby-list:40112] win32oleからのマクロの呼び出し

From: Kazuhiro Oinuma <core379@...>
Date: 2004-10-18 15:28:19 UTC
List: ruby-list #40112
生沼と申します。

win32oleを使ってExcelの自作マクロを呼び出そうとしているのですが、
引数あり & 戻り値ありのマクロを Run メソッドで呼び出した場合に、
戻り値が取得できません。引数がないマクロならば戻り値が得られるのですが
これはこういうものなのでしょうか?

具体的には以下のプログラムです。

test.rb --------------------
require 'win32ole'

file_system = WIN32OLE.new('Scripting.FileSystemObject')
file_name = file_system.getAbsolutePathName("test.xls")
excel = WIN32OLE.new('Excel.Application')
#WIN32OLE.const_load(excel, Excel)
excel.visible = true
book = excel.workbooks.open(file_name)
puts excel.run("Func1")          # ○
puts excel.run("Func2", "hoge")  # ×
excel.quit

test.xls のマクロ -------
Public Function Func1() As String
    Func1 = "Func1()"
End Function

Public Function Func2(strValue As String) As String
    Func2 = "Func2()"
End Function

実行結果 ----------------
$ ruby test.rb
Func1()
nil    <-- nilになる

なお、VBAで

 Public Sub CallFunc2()
    MsgBox Application.Run("Func2", "hoge")
 End Sub

というマクロを作ってFunc2を呼んでやると文字列"Func2"が返ってきました。

環境は Windows2000 Pro、Excel2000(9.0.2812)、rubyは
1.8.1 i386-mswin32版です。win32oleはrubyに搭載されているものを使用しています。
よろしくお願いします。

-- 
おいぬま

In This Thread

Prev Next