[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

help with require in win32ole

Li Chen

11/18/2006 3:00:00 PM

Hi all,

I recently play with win32ole library. In order to use it I put a line
at the top of the script

require 'win32ole'

so that Ruby knows where to find the library. It is my understanding I
should be able to access all the classes/modules within the file. But
this is not the case. In order to get access to ExcelConst I need to add
two more lines

module ExcelConst
end

near the top of the script.

I am a little bit confused. Why is that? Is this only special to
win32ole library or common to many Ruby libraries?

Any comments will be appriciated.

Li


And the follows are the fragment of the script:
###
require 'win32ole'

module ExcelConst
end

excel = WIN32OLE.new("Excel.Application")
workbook = excel.Workbooks.Add
excelchart = workbook.Charts.Add
....
WIN32OLE.const_load(excel, ExcelConst)
excelchart.ChartType = ExcelConst::XlConeCol

###screen output
C:\>irb
irb(main):001:0> require 'win32ole'
=> true
irb(main):002:0>
irb(main):003:0* module ExcelConst
irb(main):004:1> end
=> nil
irb(main):005:0>
irb(main):006:0* excel = WIN32OLE.new("Excel.Application")
=> #<WIN32OLE:0x2c268f0>
irb(main):007:0> workbook = excel.Workbooks.Add
=> #<WIN32OLE:0x2c24154>
irb(main):008:0> excelchart = workbook.Charts.Add
=> #<WIN32OLE:0x2c21954>
irb(main):009:0>
irb(main):010:0* WIN32OLE.const_load(excel, ExcelConst)
=> nil
irb(main):011:0> puts excelchart.ChartType = ExcelConst::XlConeCol
105
=> nil


--
Posted via http://www.ruby-....

12 Answers

Jano Svitok

11/19/2006 10:42:00 AM

0

On 11/18/06, Li Chen <chen_li3@yahoo.com> wrote:
> Hi all,
>
> I recently play with win32ole library. In order to use it I put a line
> at the top of the script
>
> require 'win32ole'
>
> so that Ruby knows where to find the library. It is my understanding I
> should be able to access all the classes/modules within the file. But
> this is not the case. In order to get access to ExcelConst I need to add
> two more lines
>
> module ExcelConst
> end
>
> near the top of the script.
>
> I am a little bit confused. Why is that? Is this only special to
> win32ole library or common to many Ruby libraries?
>
> Any comments will be appriciated.
>
> Li
>
>
> And the follows are the fragment of the script:
> ###
> require 'win32ole'
>
> module ExcelConst
> end
>
> excel = WIN32OLE.new("Excel.Application")
> workbook = excel.Workbooks.Add
> excelchart = workbook.Charts.Add
> ....
> WIN32OLE.const_load(excel, ExcelConst)
> excelchart.ChartType = ExcelConst::XlConeCol

You have to define the moduke ExcelConst, because it is not a part of
the library, it is *your* module that the library will fill with
constants (see the line with WIN32OLE.const_load). You can use any
name for it (i.e. MySuperExcelModuleForConstants). You can even reuse
any existing module for that (including any class).

If you read the documentation you can omit the module name (the second
parameter to WIN32OLE) and the constants will be added to WIN32OLE
itself.

You can add the constants to Kernel module, or Object class so that
you can use them without prefix (and thus polluting the global
namespace, although sometimes it might be handy).

Li Chen

11/19/2006 3:41:00 PM

0

Jan Svitok wrote:
> You have to define the moduke ExcelConst, because it is not a part of
> the library, it is *your* module that the library will fill with
> constants (see the line with WIN32OLE.const_load).

If it isn't part of the library of WIN32OLE, then where does it come
from, from the Excel library?

Thanks,

Li



--
Posted via http://www.ruby-....

Jano Svitok

11/19/2006 4:27:00 PM

0

On 11/19/06, Li Chen <chen_li3@yahoo.com> wrote:
> Jan Svitok wrote:
> > You have to define the moduke ExcelConst, because it is not a part of
> > the library, it is *your* module that the library will fill with
> > constants (see the line with WIN32OLE.const_load).
>
> If it isn't part of the library of WIN32OLE, then where does it come
> from, from the Excel library?

Sorry I wasn't clear enough.

It's a design decision of WIN32OLE that it doesn't create the
constants by default. However, it provides the possiblity to create
them on demand. So in the case you need the constants, you say where
you want them, and the library creates them for you. The place where
you want them is the part you have to provide (any module/class).

Obviously the constants come from the OLE object (in this case from
Excel). WIN32OLE reads them and after a bit of translation creates
them in the place you've told it.

In conclusion what I wanted to say by that line is that:

- The library will work without the ExcelConst module as happily
- It's you who has to create the module if you want the constants
- However you can name it anyway you want - e.g. XlConst is as good as
any other name
- It's possible to do without even creating the module by using
WIN32OLE or Kernel

Li Chen

11/19/2006 6:07:00 PM

0

> In conclusion what I wanted to say by that line is that:
>
> - The library will work without the ExcelConst module as happily
> - It's you who has to create the module if you want the constants
> - However you can name it anyway you want - e.g. XlConst is as good as
> any other name
> - It's possible to do without even creating the module by using
> WIN32OLE or Kernel

Hi Jan,

Thank you very much for the explanations.

Could you please give me a small example of "doing it without even
creating the module by using WIN32OLE or Kernel"?


Li


--
Posted via http://www.ruby-....

Jano Svitok

11/20/2006 9:16:00 AM

0

On 11/19/06, Li Chen <chen_li3@yahoo.com> wrote:
> > In conclusion what I wanted to say by that line is that:
> >
> > - The library will work without the ExcelConst module as happily
> > - It's you who has to create the module if you want the constants
> > - However you can name it anyway you want - e.g. XlConst is as good as
> > any other name
> > - It's possible to do without even creating the module by using
> > WIN32OLE or Kernel
>
> Hi Jan,
>
> Thank you very much for the explanations.
>
> Could you please give me a small example of "doing it without even
> creating the module by using WIN32OLE or Kernel"?

(not tested: I've made up this from documentation, the change is in
the last two lines)

require 'win32ole'

excel = WIN32OLE.new("Excel.Application")
workbook = excel.Workbooks.Add
excelchart = workbook.Charts.Add
....
WIN32OLE.const_load(excel) # missing ..., ExcelConst)
excelchart.ChartType = WIN32OLE::XlConeCol # ExcelConst -> WIN32OLE

Gustav - Railist

11/30/2006 9:17:00 AM

0

Hey

I just tried requiring 'win32ole' in my Linux console but it doesn't
seem to find it. I'm running MS Office under Linux with Crossover
Office, and was hoping I'd be able to do my MS Office development under
Linux from now on.

Would getting the win32ole libraries help at all?

Is there something small I'm missing or is this simply too ambitious?


Thanks in advance
Gustav Paul
gustav@rails.co.za

Li Chen

11/30/2006 9:52:00 AM

0

Gustav Paul wrote:
> Hey
>
> I just tried requiring 'win32ole' in my Linux console but it doesn't
> seem to find it. I'm running MS Office under Linux with Crossover
> Office, and was hoping I'd be able to do my MS Office development under
> Linux from now on.
>
> Would getting the win32ole libraries help at all?
>
> Is there something small I'm missing or is this simply too ambitious?
>
>
> Thanks in advance
> Gustav Paul
> gustav@rails.co.za

As far as I know WIN32OLE is a built-in library in the Ruby. In order to
use it you need to put this line

require 'wine32ole'

near the top of your script.

Li

--
Posted via http://www.ruby-....

Gustav - Railist

11/30/2006 10:05:00 AM

0

Li Chen wrote:
> Gustav Paul wrote:
>
>> Hey
>>
>> I just tried requiring 'win32ole' in my Linux console but it doesn't
>> seem to find it. I'm running MS Office under Linux with Crossover
>> Office, and was hoping I'd be able to do my MS Office development under
>> Linux from now on.
>>
>> Would getting the win32ole libraries help at all?
>>
>> Is there something small I'm missing or is this simply too ambitious?
>>
>>
>> Thanks in advance
>> Gustav Paul
>> gustav@rails.co.za
>>
>
> As far as I know WIN32OLE is a built-in library in the Ruby. In order to
> use it you need to put this line
>
> require 'wine32ole'
>
> near the top of your script.
>
> Li
>
>
Hi

Thanks for the reply. I tried it in irb, but I get

irb(main):001:0> require 'win32ole'
LoadError: no such file to load -- win32ole
from (irb):1:in `require'
from (irb):1

I tried 'wine32ole' as well, same problem.
I noticed that on my Windows machine I've got
c:\ruby\lib\ruby\1.8\win32
c:\ruby\lib\ruby\1.8\win32ole

Directories, which I can't find under
/usr/lib/ruby/1.8

Might it be necessary to download the win32ole library?
Is there one for Linux?

Thanks, I'd really love to be able to develop from Linux, but if all
else fails, I suppose I'll make do with Windows :]

Gustav


Jano Svitok

11/30/2006 10:20:00 AM

0

On 11/30/06, Gustav Paul <gustav@rails.co.za> wrote:
> Li Chen wrote:
> > Gustav Paul wrote:
> >
> >> Hey
> >>
> >> I just tried requiring 'win32ole' in my Linux console but it doesn't
> >> seem to find it. I'm running MS Office under Linux with Crossover
> >> Office, and was hoping I'd be able to do my MS Office development under
> >> Linux from now on.
> >>
> >> Would getting the win32ole libraries help at all?
> >>
> >> Is there something small I'm missing or is this simply too ambitious?
> >>
> >>
> >> Thanks in advance
> >> Gustav Paul
> >> gustav@rails.co.za
> >>
> >
> > As far as I know WIN32OLE is a built-in library in the Ruby. In order to
> > use it you need to put this line
> >
> > require 'wine32ole'
> >
> > near the top of your script.
> >
> > Li
> >
> >
> Hi
>
> Thanks for the reply. I tried it in irb, but I get
>
> irb(main):001:0> require 'win32ole'
> LoadError: no such file to load -- win32ole
> from (irb):1:in `require'
> from (irb):1
>
> I tried 'wine32ole' as well, same problem.
> I noticed that on my Windows machine I've got
> c:\ruby\lib\ruby\1.8\win32
> c:\ruby\lib\ruby\1.8\win32ole
>
> Directories, which I can't find under
> /usr/lib/ruby/1.8
>
> Might it be necessary to download the win32ole library?
> Is there one for Linux?
>
> Thanks, I'd really love to be able to develop from Linux, but if all
> else fails, I suppose I'll make do with Windows :]

The problem is, that win32ole is a windows extension, you have linux
ruby and office running under wine. So you have to make them talk to
each other. You need to somehow compile the win32ole extension under
linux to use wine to talk to msoffice. I would say that:

1. it should be possible
2. many people will thank you for that ;-)
3. certainly it is not easy - you'd need to know a bit of ruby
internals, a bit of wine, a bit of OLE and you'd need to write C code
during the process.

Gustav - Railist

11/30/2006 10:34:00 AM

0


Hi
> The problem is, that win32ole is a windows extension, you have linux
> ruby and office running under wine. So you have to make them talk to
> each other. You need to somehow compile the win32ole extension under
> linux to use wine to talk to msoffice. I would say that:
>
> 1. it should be possible
> 2. many people will thank you for that ;-)
> 3. certainly it is not easy - you'd need to know a bit of ruby
> internals, a bit of wine, a bit of OLE and you'd need to write C code
> during the process.
>
Thanks, I can see why people would be thankful!
I'll give it a shot, can't be that difficult ;]

I had a vague inkling that it wouldn't be simple,
Thanks for the reply!

Gustav