[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to translate a VBA code into Ruby script

Li Chen

11/20/2006 1:14:00 PM

Hi all,

I need to use WIN32OLE to do some Excel automation. I read some
documents and search google.The examples of how to use WIN32OLE are
almost the same. But none of them show how to translate a VBA code into
Ruby. Here are the VBA codes:

Sub AddNew()
Set NewBook = Workbooks.Add
With NewBook
.Title = "All Sales"
.Subject = "Sales"
.SaveAs Filename:="Allsales.xls"
End With
End Sub


And here are some Ruby codes:
require 'win32ole'

excel=WIN32OLE.new('Excel.Application')
workbook=excel.Workbooks.Add
worksheet=workbook.Worksheets.Add

I wonder if any expert out there could give me an example on how to
translate Title, Subject, and SaveAs Filename into to Ruby codes.

Thanks,

Li




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

3 Answers

Jano Svitok

11/20/2006 1:33:00 PM

0

On 11/20/06, Li Chen <chen_li3@yahoo.com> wrote:
> Hi all,
>
> I need to use WIN32OLE to do some Excel automation. I read some
> documents and search google.The examples of how to use WIN32OLE are
> almost the same. But none of them show how to translate a VBA code into
> Ruby. Here are the VBA codes:
>
> Sub AddNew()
> Set NewBook = Workbooks.Add
> With NewBook
> .Title = "All Sales"
> .Subject = "Sales"
> .SaveAs Filename:="Allsales.xls"
> End With
> End Sub
>
>
> And here are some Ruby codes:
require 'win32ole'

excel=WIN32OLE.new('Excel.Application')
#Set NewBook = Workbooks.Add
newbook=excel.Workbooks.Add
#With NewBook
# .Title = "All Sales"
newbook.Title = "All Sales"
# .Subject = "Sales"
newbook.Subject = "Sales"
# .SaveAs Filename:="Allsales.xls"
newbook.SaveAs Filename => "Allsales.xls"

or

newbook['Title'] = "All Sales"
newbook['Subject'] = "Sales"
newbook.SaveAs Filename => "Allsales.xls"

please read http://ruby-doc.org/docs/ProgrammingRuby/html/...
and http://ruby-doc.org/stdlib/libdoc/win32ole/rdoc/...

Li Chen

11/20/2006 2:22:00 PM

0

Hi Jan,

Thank you very much. I think I am on the right track now.

Li

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

benjohn

11/20/2006 4:12:00 PM

0

> On 11/20/06, Li Chen <chen_li3@yahoo.com> wrote:
>> Hi all,
>>
>> I need to use WIN32OLE to do some Excel automation. I read some
>> documents and search google.The examples of how to use WIN32OLE are
>> almost the same. But none of them show how to translate a VBA code
>> into
>> Ruby. Here are the VBA codes:
>>
>> Sub AddNew()
>> Set NewBook = Workbooks.Add
>> With NewBook
>> .Title = "All Sales"
>> .Subject = "Sales"
>> .SaveAs Filename:="Allsales.xls"
>> End With
>> End Sub
>>
>>
>> And here are some Ruby codes:
> require 'win32ole'
>
> excel=WIN32OLE.new('Excel.Application')
> #Set NewBook = Workbooks.Add
> newbook=excel.Workbooks.Add
> #With NewBook
> # .Title = "All Sales"
> newbook.Title = "All Sales"
> # .Subject = "Sales"
> newbook.Subject = "Sales"
> # .SaveAs Filename:="Allsales.xls"
> newbook.SaveAs Filename => "Allsales.xls"

I expect you can also do:
newbook=excel.Workbooks.Add
newbook.instance_eval do
Title = "All Sales"
Subject = "Sales"
SaveAs = Filename:="Allsales.xls"
end

:) But I'm guessing. I'd be interested to know if it works, but it would
be a closer mapping to VB's "with" expression.