Object
WIN32OLE
objects represent OLE Automation object in Ruby.
Returns current codepage.
WIN32OLE.codepage # => WIN32OLE::CP_ACP
static VALUE fole_s_get_code_page(self) VALUE self; { return INT2FIX(cWIN32OLE_cp); }
Sets current codepage.
WIN32OLE.codepage = WIN32OLE::CP_UTF8
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; } /* * Should this method return old codepage? */ return Qnil; }
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.
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)); } /* get CLSID from OLE server name */ pBuf = ole_mb2wc(StringValuePtr(svr_name), -1); hr = CLSIDFromProgID(pBuf, &clsid); if(FAILED(hr)) { hr = CLSIDFromString(pBuf, &clsid); } SysFreeString(pBuf); if(FAILED(hr)) { return ole_bind_obj(svr_name, argc, argv, self); } hr = GetActiveObject(&clsid, 0, &pUnknown); if (FAILED(hr)) { ole_raise(hr, eWIN32OLE_RUNTIME_ERROR, "OLE server `%s' not running", StringValuePtr(svr_name)); } hr = pUnknown->lpVtbl->QueryInterface(pUnknown, &IID_IDispatch, (void **)&pDispatch); if(FAILED(hr)) { OLE_RELEASE(pUnknown); ole_raise(hr, eWIN32OLE_RUNTIME_ERROR, "failed to create WIN32OLE server `%s'", StringValuePtr(svr_name)); } OLE_RELEASE(pUnknown); return create_win32ole_object(self, pDispatch, argc, argv); }
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
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"); } if (rb_obj_is_kind_of(ole, cWIN32OLE)) { OLEData_Get_Struct(ole, pole); hr = pole->pDispatch->lpVtbl->GetTypeInfo(pole->pDispatch, 0, lcid, &pTypeInfo); if(FAILED(hr)) { ole_raise(hr, rb_eRuntimeError, "failed to GetTypeInfo"); } hr = pTypeInfo->lpVtbl->GetContainingTypeLib(pTypeInfo, &pTypeLib, &index); if(FAILED(hr)) { OLE_RELEASE(pTypeInfo); ole_raise(hr, rb_eRuntimeError, "failed to GetContainingTypeLib"); } OLE_RELEASE(pTypeInfo); if(TYPE(klass) != T_NIL) { ole_const_load(pTypeLib, klass, self); } else { ole_const_load(pTypeLib, cWIN32OLE, self); } OLE_RELEASE(pTypeLib); } else if(TYPE(ole) == T_STRING) { file = typelib_file(ole); if (file == Qnil) { file = ole; } pBuf = ole_mb2wc(StringValuePtr(file), -1); hr = LoadTypeLibEx(pBuf, REGKIND_NONE, &pTypeLib); SysFreeString(pBuf); if (FAILED(hr)) ole_raise(hr, eWIN32OLE_RUNTIME_ERROR, "failed to LoadTypeLibEx"); if(TYPE(klass) != T_NIL) { ole_const_load(pTypeLib, klass, self); } else { ole_const_load(pTypeLib, cWIN32OLE, self); } OLE_RELEASE(pTypeLib); } else { rb_raise(rb_eTypeError, "1st parameter must be WIN32OLE instance"); } return Qnil; }
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.
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)); } if (!NIL_P(host)) { if (ruby_safe_level > 0 && OBJ_TAINTED(host)) { rb_raise(rb_eSecurityError, "Insecure Object Creation - %s", StringValuePtr(svr_name)); } return ole_create_dcom(argc, argv, self); } /* get CLSID from OLE server name */ pBuf = ole_mb2wc(StringValuePtr(svr_name), -1); hr = CLSIDFromProgID(pBuf, &clsid); if(FAILED(hr)) { hr = CLSIDFromString(pBuf, &clsid); } SysFreeString(pBuf); if(FAILED(hr)) { ole_raise(hr, eWIN32OLE_RUNTIME_ERROR, "unknown OLE server: `%s'", StringValuePtr(svr_name)); } /* get IDispatch interface */ hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, &IID_IDispatch, (void**)&pDispatch); if(FAILED(hr)) { ole_raise(hr, eWIN32OLE_RUNTIME_ERROR, "failed to create WIN32OLE object from `%s'", StringValuePtr(svr_name)); } ole_set_member(self, pDispatch); return self; }
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.
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); } } return INT2NUM(n); }
Returns reference counter of Dispatch interface of WIN32OLE object. You should not use this method because this method exists only for debugging WIN32OLE.
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)); }
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)
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)); } helpcontext = rb_funcall(target, rb_intern("helpcontext"), 0); } else { helpfile = target; } if (TYPE(helpfile) != T_STRING) { rb_raise(rb_eTypeError, "1st parameter must be (String|WIN32OLE_TYPE|WIN32OLE_METHOD)"); } hwnd = ole_show_help(helpfile, helpcontext); if(hwnd == 0) { rb_raise(rb_eRuntimeError, "failed to open help file `%s'", StringValuePtr(helpfile)); } return Qnil; }
Returns property of OLE object.
excel = WIN32OLE.new('Excel.Application') puts excel['Visible'] # => false
static VALUE fole_getproperty(argc, argv, self) int argc; VALUE *argv; VALUE self; { return ole_invoke(argc, argv, self, DISPATCH_PROPERTYGET); }
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.
static VALUE fole_setproperty(argc, argv, self) int argc; VALUE *argv; VALUE self; { return ole_invoke(argc, argv, self, DISPATCH_PROPERTYPUT); }
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
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); }
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
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); }
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
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); }
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
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"); } if (V_VT(&result) == VT_UNKNOWN) hr = V_UNKNOWN(&result)->lpVtbl->QueryInterface(V_UNKNOWN(&result), &IID_IEnumVARIANT, (void**)&pEnum); else if (V_VT(&result) == VT_DISPATCH) hr = V_DISPATCH(&result)->lpVtbl->QueryInterface(V_DISPATCH(&result), &IID_IEnumVARIANT, (void**)&pEnum); if (FAILED(hr) || !pEnum) { VariantClear(&result); ole_raise(hr, rb_eRuntimeError, "failed to get IEnum Interface"); } VariantClear(&result); rb_ensure(ole_each_sub, (VALUE)pEnum, ole_ienum_free, (VALUE)pEnum); return Qnil; }
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
static VALUE fole_invoke(argc, argv, self) int argc; VALUE *argv; VALUE self; { return ole_invoke(argc, argv, self, DISPATCH_METHOD|DISPATCH_PROPERTYGET); }
Calls #invoke method.
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"); } n = strlen(mname); if(mname[n-1] == '=') { argv[0] = rb_str_new(mname, n-1); return ole_propertyput(self, argv[0], argv[1]); } else { argv[0] = rb_str_new2(mname); return ole_invoke(argc, argv, self, DISPATCH_METHOD|DISPATCH_PROPERTYGET); } }
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.
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; }
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
static VALUE fole_func_methods( self ) VALUE self; { return ole_methods( self, INVOKE_FUNC); }
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
static VALUE fole_get_methods( self ) VALUE self; { return ole_methods( self, INVOKE_PROPERTYGET); }
Returns WIN32OLE_METHOD object corresponding with method specified by 1st argument.
excel = WIN32OLE.new('Excel.Application') method = excel.ole_method_help('Quit')
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; }
Returns the array of WIN32OLE_METHOD object. The element is OLE method of WIN32OLE object.
excel = WIN32OLE.new('Excel.Application') methods = excel.ole_methods
static VALUE fole_methods( self ) VALUE self; { return ole_methods( self, INVOKE_FUNC | INVOKE_PROPERTYGET | INVOKE_PROPERTYPUT | INVOKE_PROPERTYPUTREF); }
Returns WIN32OLE_TYPE object.
excel = WIN32OLE.new('Excel.Application') tobj = excel.ole_obj_help
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"); } hr = pTypeInfo->lpVtbl->GetContainingTypeLib( pTypeInfo, &pTypeLib, &index ); if(FAILED(hr)) { OLE_RELEASE(pTypeInfo); ole_raise(hr, rb_eRuntimeError, "failed to GetContainingTypeLib"); } hr = pTypeLib->lpVtbl->GetDocumentation( pTypeLib, index, &bstr, NULL, NULL, NULL); if (SUCCEEDED(hr)) { type = foletype_s_allocate(cWIN32OLE_TYPE); oletype_set_member(type, pTypeInfo, WC2VSTR(bstr)); } OLE_RELEASE(pTypeLib); OLE_RELEASE(pTypeInfo); return type; }
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
static VALUE fole_put_methods( self ) VALUE self; { return ole_methods( self, INVOKE_PROPERTYPUT | INVOKE_PROPERTYPUTREF); }
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.
static VALUE fole_setproperty(argc, argv, self) int argc; VALUE *argv; VALUE self; { return ole_invoke(argc, argv, self, DISPATCH_PROPERTYPUT); }