Class: WIN32OLE

Inherits:
Object
  • Object
show all
Defined in:
win32ole.c,
win32ole.c

Overview

WIN32OLE objects represent OLE Automation object in Ruby.

Direct Known Subclasses

MyExcel

Defined Under Namespace

Modules: VARIANT

Constant Summary collapse

VERSION =
rb_str_new2(WIN32OLE_VERSION)
ARGV =
rb_ary_new()
CP_THREAD_ACP =
INT2FIX(CP_THREAD_ACP)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(server, [host]) ⇒ WIN32OLE object

Returns a new WIN32OLE object(OLE Automation object). The first argument server specifies OLE Automation server. The first argument should be CLSID or PROGID. If second argument host specified, then returns OLE Automation object on host.

WIN32OLE.new('Excel.Application') # => Excel OLE Automation WIN32OLE object.
WIN32OLE.new('{00024500-0000-0000-C000-000000000046}') # => Excel OLE Automation WIN32OLE object.


1867
1868
1869
# File 'win32ole.c', line 1867

static VALUE
fole_initialize(argc, argv, self)
int argc;

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id[,arg1, arg2, ...]) ⇒ Object

Calls WIN32OLE#invoke method.



2675
2676
2677
# File 'win32ole.c', line 2675

static VALUE
fole_missing(argc, argv, self)
int argc;

Class Method Details

.codepageObject

Returns current codepage.

WIN32OLE.codepage # => WIN32OLE::CP_ACP


1806
1807
1808
# File 'win32ole.c', line 1806

static VALUE
fole_s_get_code_page(self)
VALUE self;

.codepage=(CP) ⇒ Object

Sets current codepage.

WIN32OLE.codepage = WIN32OLE::CP_UTF8


1820
1821
1822
# File 'win32ole.c', line 1820

static VALUE
fole_s_set_code_page(self, vcp)
VALUE self;

.connect(ole) ⇒ Object

Returns running OLE Automation object or WIN32OLE object from moniker. 1st argument should be OLE program id or class id or moniker.

WIN32OLE.connect('Excel.Application') # => WIN32OLE object which represents running Excel.


1489
1490
1491
# File 'win32ole.c', line 1489

static VALUE
fole_s_connect(argc, argv, self)
int argc;

.const_load(ole, mod = WIN32OLE) ⇒ Object

Defines the constants of OLE Automation server as mod’s constants. The first argument is WIN32OLE object or type library name. If 2nd argument is omitted, the default is WIN32OLE. The first letter of Ruby’s constant variable name is upper case, so constant variable name of WIN32OLE object is capitalized. For example, the ‘xlTop’ constant of Excel is changed to ‘XlTop’ in WIN32OLE. If the first letter of constant variabl is not [A-Z], then the constant is defined as CONSTANTS hash element.

module EXCEL_CONST
end
excel = WIN32OLE.new('Excel.Application')
WIN32OLE.const_load(excel, EXCEL_CONST)
puts EXCEL_CONST::XlTop # => -4160
puts EXCEL_CONST::CONSTANTS['_xlDialogChartSourceData'] # => 541

WIN32OLE.const_load(excel)
puts WIN32OLE::XlTop # => -4160

module MSO
end
WIN32OLE.const_load('Microsoft Office 9.0 Object Library', MSO)
puts MSO::MsoLineSingle # => 1


1572
1573
1574
# File 'win32ole.c', line 1572

static VALUE
fole_s_const_load(argc, argv, self)
int argc;

.ole_free(aWIN32OLE) ⇒ Object

Invokes Release method of Dispatch interface of WIN32OLE object. You should not use this method because this method exists only for debugging WIN32OLE. The return value is reference counter of OLE object.



1714
1715
1716
# File 'win32ole.c', line 1714

static VALUE
fole_s_free(self, obj)
VALUE self;

.ole_reference_count(aWIN32OLE) ⇒ Object

Returns reference counter of Dispatch interface of WIN32OLE object. You should not use this method because this method exists only for debugging WIN32OLE.



1695
1696
1697
# File 'win32ole.c', line 1695

static VALUE
fole_s_reference_count(self, obj)
VALUE self;

.ole_show_help(obj[,helpcontext]) ⇒ Object

Displays helpfile. The 1st argument specifies WIN32OLE_TYPE object or WIN32OLE_METHOD object or helpfile.

excel = WIN32OLE.new('Excel.Application')
typeobj = excel.ole_type
WIN32OLE.ole_show_help(typeobj)


1764
1765
1766
# File 'win32ole.c', line 1764

static VALUE
fole_s_show_help(argc, argv, self)
int argc;

Instance Method Details

#[]('property') ⇒ Object

Returns property of OLE object.

excel = WIN32OLE.new('Excel.Application')
puts excel['Visible'] # => false


2491
2492
2493
# File 'win32ole.c', line 2491

static VALUE
fole_getproperty(argc, argv, self)
int argc;

#[]=('property') ⇒ Object #setproperty('property', [arg1, arg2,...]) ⇒ Object

Sets property of OLE object. When you want to set property with argument, you can use this method.

excel = WIN32OLE.new('Excel.Application')
excel['Visible'] = true
book = excel.workbooks.add
sheet = book.worksheets(1)
sheet.setproperty('Cells', 1, 2, 10) # => The B1 cell value is 10.


2473
2474
2475
# File 'win32ole.c', line 2473

static VALUE
fole_setproperty(argc, argv, self)
int argc;

#_getproperty(dispid, args, types) ⇒ Object

Runs the early binding method to get property. The 1st argument specifies dispatch ID, the 2nd argument specifies the array of arguments, the 3rd argument specifies the array of the type of arguments.

excel = WIN32OLE.new('Excel.Application')
puts excel._getproperty(558, [], []) # same effect as puts excel.visible


2427
2428
2429
# File 'win32ole.c', line 2427

static VALUE
fole_getproperty2(self, dispid, args, types)
VALUE self;

#_invoke(dispid, args, types) ⇒ Object

Runs the early binding method. The 1st argument specifies dispatch ID, the 2nd argument specifies the array of arguments, the 3rd argument specifies the array of the type of arguments.

excel = WIN32OLE.new('Excel.Application')
excel._invoke(302, [], []) #  same effect as excel.Quit


2405
2406
2407
# File 'win32ole.c', line 2405

static VALUE
fole_invoke2(self, dispid, args, types)
VALUE self;

#_setproperty(dispid, args, types) ⇒ Object

Runs the early binding method to set property. The 1st argument specifies dispatch ID, the 2nd argument specifies the array of arguments, the 3rd argument specifies the array of the type of arguments.

excel = WIN32OLE.new('Excel.Application')
excel._setproperty(558, [true], [WIN32OLE::VARIANT::VT_BOOL]) # same effect as excel.visible = true


2449
2450
2451
# File 'win32ole.c', line 2449

static VALUE
fole_setproperty2(self, dispid, args, types)
VALUE self;

#each {|i| ... } ⇒ Object

Iterates over each item of OLE collection which has IEnumVARIANT interface.

excel = WIN32OLE.new('Excel.Application')
book = excel.workbooks.add
sheets = book.worksheets(1)
cells = sheets.cells("A1:A5")
cells.each do |cell|
  cell.value = 10
end

Yields:

  • (i)


2617
2618
2619
# File 'win32ole.c', line 2617

static VALUE
fole_each(self)
VALUE self;

#invoke(method, [arg1,...]) ⇒ Object

Runs OLE method. The first argument specifies the method name of OLE Automation object. The others specify argument of the method. If you can not execute method directly, then use this method instead.

excel = WIN32OLE.new('Excel.Application')
excel.invoke('Quit')  # => same as excel.Quit


2190
2191
2192
# File 'win32ole.c', line 2190

static VALUE
fole_invoke(argc, argv, self)
int argc;

#ole_activex_initializeQnil

Initialize WIN32OLE object(ActiveX Control) by calling IPersistMemory::InitNew.

Before calling OLE method, some kind of the ActiveX controls created with MFC should be initialized by calling IPersistXXX::InitNew.

If and only if you recieved the exception “HRESULT error code: 0x8000ffff catastrophic failure”, try this method before invoking any ole_method.

obj = WIN32OLE.new("ProgID_or_GUID_of_ActiveX_Control")
obj.ole_activex_initialize
obj.method(...)

Returns:

  • (Qnil)


3284
3285
3286
# File 'win32ole.c', line 3284

static VALUE
fole_activex_initialize(self)
VALUE self;

#ole_freeObject

invokes Release method of Dispatch interface of WIN32OLE object. Usually, you do not need to call this method because Release method called automatically when WIN32OLE object garbaged.



2565
2566
2567
# File 'win32ole.c', line 2565

static VALUE
fole_free(self)
VALUE self;

#ole_func_methodsObject

Returns the array of WIN32OLE_METHOD object . The element of the array is functional method of WIN32OLE object.

excel = WIN32OLE.new('Excel.Application')
properties = excel.ole_func_methods


2978
2979
2980
# File 'win32ole.c', line 2978

static VALUE
fole_func_methods( self )
VALUE self;

#ole_get_methodsObject

Returns the array of WIN32OLE_METHOD object . The element of the array is property (gettable) of WIN32OLE object.

excel = WIN32OLE.new('Excel.Application')
properties = excel.ole_get_methods


2943
2944
2945
# File 'win32ole.c', line 2943

static VALUE
fole_get_methods( self )
VALUE self;

#ole_method_help(method) ⇒ Object Also known as: ole_method_help

Returns WIN32OLE_METHOD object corresponding with method specified by 1st argument.

excel = WIN32OLE.new('Excel.Application')
method = excel.ole_method_help('Quit')


3239
3240
3241
# File 'win32ole.c', line 3239

static VALUE
fole_method_help( self, cmdname )
VALUE self;

#ole_methodsObject

Returns the array of WIN32OLE_METHOD object. The element is OLE method of WIN32OLE object.

excel = WIN32OLE.new('Excel.Application')
methods = excel.ole_methods


2926
2927
2928
# File 'win32ole.c', line 2926

static VALUE
fole_methods( self )
VALUE self;

#ole_obj_helpObject

Returns WIN32OLE_TYPE object.

excel = WIN32OLE.new('Excel.Application')
tobj = excel.ole_obj_help


2994
2995
2996
# File 'win32ole.c', line 2994

static VALUE
fole_obj_help( self )
VALUE self;

#ole_put_methodsObject

Returns the array of WIN32OLE_METHOD object . The element of the array is property (settable) of WIN32OLE object.

excel = WIN32OLE.new('Excel.Application')
properties = excel.ole_put_methods


2960
2961
2962
# File 'win32ole.c', line 2960

static VALUE
fole_put_methods( self )
VALUE self;

#[]=('property') ⇒ Object #setproperty('property', [arg1, arg2,...]) ⇒ Object

Sets property of OLE object. When you want to set property with argument, you can use this method.

excel = WIN32OLE.new('Excel.Application')
excel['Visible'] = true
book = excel.workbooks.add
sheet = book.worksheets(1)
sheet.setproperty('Cells', 1, 2, 10) # => The B1 cell value is 10.


2473
2474
2475
# File 'win32ole.c', line 2473

static VALUE
fole_setproperty(argc, argv, self)
int argc;