Class: WIN32OLE

Inherits:
Object
  • Object
show all
Defined in:
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.


# File 'win32ole.c'

static VALUE
fole_initialize(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE svr_name;
VALUE host;
VALUE others;
HRESULT hr;
CLSID   clsid;
OLECHAR *pBuf;
IDispatch *pDispatch;

rb_secure(4);
rb_call_super(0, 0);
rb_scan_args(argc, argv, "11*", &svr_name, &host, &others);

if (ruby_safe_level > 0 && OBJ_TAINTED(svr_name)) {
    rb_raise(rb_eSecurityError, "Insecure Object Creation - %s",
             StringValuePtr(svr_name));
}

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Calls WIN32OLE#invoke method.



# File 'win32ole.c'

static VALUE
fole_missing(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
ID id;
char* mname;
int n;
id = rb_to_id(argv[0]);
mname = rb_id2name(id);
if(!mname) {
    rb_raise(rb_eRuntimeError, "fail: unknown method or property");
}

Class Method Details

.codepageObject

Returns current codepage.

WIN32OLE.codepage # => WIN32OLE::CP_ACP


# File 'win32ole.c'

static VALUE
fole_s_get_code_page(self)
    VALUE self;
{
    return INT2FIX(cWIN32OLE_cp);
}

.codepage=(CP) ⇒ Object

Sets current codepage.

WIN32OLE.codepage = WIN32OLE::CP_UTF8


# File 'win32ole.c'

static VALUE
fole_s_set_code_page(self, vcp)
VALUE self;
VALUE vcp;
{
UINT cp = FIX2INT(vcp);

switch(cp) {
case CP_ACP:
case CP_OEMCP:
case CP_MACCP:
case CP_THREAD_ACP:
case CP_SYMBOL:
case CP_UTF7:
case CP_UTF8:
    cWIN32OLE_cp = cp;
    break;
default:
    rb_raise(eWIN32OLE_RUNTIME_ERROR, "codepage should be WIN32OLE::CP_ACP, WIN32OLE::CP_OEMCP, WIN32OLE::CP_MACCP, WIN32OLE::CP_THREAD_ACP, WIN32OLE::CP_SYMBOL, WIN32OLE::CP_UTF7, WIN32OLE::CP_UTF8");
    break;
}

.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.


# File 'win32ole.c'

static VALUE
fole_s_connect(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE svr_name;
VALUE others;
HRESULT hr;
CLSID   clsid;
OLECHAR *pBuf;
IDispatch *pDispatch;
IUnknown *pUnknown;

rb_secure(4);
/* initialize to use OLE */
ole_initialize();

rb_scan_args(argc, argv, "1*", &svr_name, &others);
if (ruby_safe_level > 0 && OBJ_TAINTED(svr_name)) {
    rb_raise(rb_eSecurityError, "Insecure Object Connection - %s",
             StringValuePtr(svr_name));
}

.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


# File 'win32ole.c'

static VALUE
fole_s_const_load(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE ole;
VALUE klass;
struct oledata *pole;
ITypeInfo *pTypeInfo;
ITypeLib *pTypeLib;
unsigned int index;
HRESULT hr;
OLECHAR *pBuf;
VALUE file;
LCID    lcid = LOCALE_SYSTEM_DEFAULT;

rb_secure(4);
rb_scan_args(argc, argv, "11", &ole, &klass);
if (TYPE(klass) != T_CLASS &&
    TYPE(klass) != T_MODULE &&
    TYPE(klass) != T_NIL) {
    rb_raise(rb_eTypeError, "2nd parameter must be Class or Module");
}

.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.



# File 'win32ole.c'

static VALUE
fole_s_free(self, obj)
    VALUE self;
    VALUE obj;
{
    ULONG n = 0;
    struct oledata * pole;
    OLEData_Get_Struct(obj, pole);
    if(pole->pDispatch) {
if (reference_count(pole) > 0) {
    n = OLE_RELEASE(pole->pDispatch);
}

.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.



# File 'win32ole.c'

static VALUE
fole_s_reference_count(self, obj)
    VALUE self;
    VALUE obj;
{
    struct oledata * pole;
    OLEData_Get_Struct(obj, pole);
    return INT2NUM(reference_count(pole));
}

.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)


# File 'win32ole.c'

static VALUE
fole_s_show_help(argc, argv, self)
    int argc;
    VALUE *argv;
    VALUE self;
{
    VALUE target;
    VALUE helpcontext;
    VALUE helpfile;
    VALUE name;
    HWND  hwnd;
    rb_scan_args(argc, argv, "11", &target, &helpcontext);
    if (rb_obj_is_kind_of(target, cWIN32OLE_TYPE) ||
rb_obj_is_kind_of(target, cWIN32OLE_METHOD)) {
helpfile = rb_funcall(target, rb_intern("helpfile"), 0);
if(strlen(StringValuePtr(helpfile)) == 0) {
    name = rb_ivar_get(target, rb_intern("name"));
    rb_raise(rb_eRuntimeError, "no helpfile of `%s'",
             StringValuePtr(name));
}

Instance Method Details

#[]('property') ⇒ Object

Returns property of OLE object.

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


# File 'win32ole.c'

static VALUE
fole_getproperty(argc, argv, self)
    int argc;
    VALUE *argv;
    VALUE self;
{
    return ole_invoke(argc, argv, self, DISPATCH_PROPERTYGET);
}

#[]=('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.


# File 'win32ole.c'

static VALUE
fole_setproperty(argc, argv, self)
    int argc;
    VALUE *argv;
    VALUE self;
{
    return ole_invoke(argc, argv, self, DISPATCH_PROPERTYPUT);
}

#_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


# File 'win32ole.c'

static VALUE
fole_getproperty2(self, dispid, args, types)
    VALUE self;
    VALUE dispid;
    VALUE args;
    VALUE types;
{
    return ole_invoke2(self, dispid, args, types, DISPATCH_PROPERTYGET);
}

#_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


# File 'win32ole.c'

static VALUE
fole_invoke2(self, dispid, args, types)
    VALUE self;
    VALUE dispid;
    VALUE args;
    VALUE types;
{
    return ole_invoke2(self, dispid, args, types, DISPATCH_METHOD);
}

#_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


# File 'win32ole.c'

static VALUE
fole_setproperty2(self, dispid, args, types)
    VALUE self;
    VALUE dispid;
    VALUE args;
    VALUE types;
{
    return ole_invoke2(self, dispid, args, types, DISPATCH_PROPERTYPUT);
}

#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)


# File 'win32ole.c'

static VALUE
fole_each(self)
VALUE self;
{
LCID    lcid = LOCALE_SYSTEM_DEFAULT;

struct oledata *pole;

unsigned int argErr;
EXCEPINFO excepinfo;
DISPPARAMS dispParams;
VARIANT result;
HRESULT hr;
IEnumVARIANT *pEnum = NULL;

VariantInit(&result);
dispParams.rgvarg = NULL;
dispParams.rgdispidNamedArgs = NULL;
dispParams.cNamedArgs = 0;
dispParams.cArgs = 0;
memset(&excepinfo, 0, sizeof(excepinfo));

OLEData_Get_Struct(self, pole);
hr = pole->pDispatch->lpVtbl->Invoke(pole->pDispatch, DISPID_NEWENUM,
                                     &IID_NULL, lcid,
                                     DISPATCH_METHOD | DISPATCH_PROPERTYGET,
                                     &dispParams, &result,
                                     &excepinfo, &argErr);

if (FAILED(hr)) {
    VariantClear(&result);
    ole_raise(hr, eWIN32OLE_RUNTIME_ERROR, "failed to get IEnum Interface");
}

#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


# File 'win32ole.c'

static VALUE
fole_invoke(argc, argv, self)
    int argc;
    VALUE *argv;
    VALUE self;
{
    return ole_invoke(argc, argv, self, DISPATCH_METHOD|DISPATCH_PROPERTYGET);
}

#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.



# File 'win32ole.c'

static VALUE
fole_free(self)
    VALUE self;
{
    struct oledata *pole;
    rb_secure(4);
    OLEData_Get_Struct(self, pole);
    OLE_FREE(pole->pDispatch);
    pole->pDispatch = NULL;
    return Qnil;
}

#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


# File 'win32ole.c'

static VALUE
fole_func_methods( self )
    VALUE self;
{
    return ole_methods( self, INVOKE_FUNC);
}

#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


# File 'win32ole.c'

static VALUE
fole_get_methods( self )
    VALUE self;
{
    return ole_methods( self, INVOKE_PROPERTYGET);
}

#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')


# File 'win32ole.c'

static VALUE
fole_method_help( self, cmdname )
    VALUE self;
    VALUE cmdname;
{
    ITypeInfo *pTypeInfo;
    HRESULT hr;
    struct oledata *pole;
    VALUE method, obj;
    LCID    lcid = LOCALE_SYSTEM_DEFAULT;

    Check_SafeStr(cmdname);
    OLEData_Get_Struct(self, pole);
    hr = typeinfo_from_ole(pole, &pTypeInfo);
    if(FAILED(hr))
        ole_raise(hr, rb_eRuntimeError, "failed to get ITypeInfo");
    method = folemethod_s_allocate(cWIN32OLE_METHOD);
    obj = olemethod_from_typeinfo(method, pTypeInfo, cmdname);
    OLE_RELEASE(pTypeInfo);
    if (obj == Qnil)
        rb_raise(eWIN32OLE_RUNTIME_ERROR, "not found %s",
                 StringValuePtr(cmdname));
    return obj;
}

#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


# File 'win32ole.c'

static VALUE
fole_methods( self )
    VALUE self;
{
    return ole_methods( self, INVOKE_FUNC | INVOKE_PROPERTYGET | INVOKE_PROPERTYPUT | INVOKE_PROPERTYPUTREF);
}

#ole_obj_helpObject

Returns WIN32OLE_TYPE object.

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


# File 'win32ole.c'

static VALUE
fole_obj_help( self )
VALUE self;
{
unsigned int index;
ITypeInfo *pTypeInfo;
ITypeLib *pTypeLib;
HRESULT hr;
struct oledata *pole;
BSTR bstr;
LCID  lcid = LOCALE_SYSTEM_DEFAULT;
VALUE type = Qnil;

OLEData_Get_Struct(self, pole);

hr = pole->pDispatch->lpVtbl->GetTypeInfo( pole->pDispatch, 0, lcid, &pTypeInfo );
if(FAILED(hr)) {
    ole_raise(hr, rb_eRuntimeError, "failed to GetTypeInfo");
}

#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


# File 'win32ole.c'

static VALUE
fole_put_methods( self )
    VALUE self;
{
    return ole_methods( self, INVOKE_PROPERTYPUT | INVOKE_PROPERTYPUTREF);
}

#[]=('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.


# File 'win32ole.c'

static VALUE
fole_setproperty(argc, argv, self)
    int argc;
    VALUE *argv;
    VALUE self;
{
    return ole_invoke(argc, argv, self, DISPATCH_PROPERTYPUT);
}