Showing posts with label vb. Show all posts
Showing posts with label vb. Show all posts

Friday, February 16, 2007

From VB to Ruby - Creating Objects via COM

Many developers looking to take up Ruby on MS Windows have experience with Visual Basic, VBA and VBScript. There are millions of examples of VB code out there, and these can help you get up to speed with Ruby, especially in areas such as Microsoft Office automation. Here are a few tips when moving from VB to Ruby:

  • Require the win32ole library at the top of your script.
  • VB requires the Set statement when binding an object to a variable; Ruby does not.
  • In place of VB's CreateObject function, use Ruby's WIN32OLE.new method.
  • In place of VB's GetObject function, use Ruby's WIN32OLE.connect method to connect to an existing instance of an object.
  • Remove all Dim statements; Ruby does not use them.
So...

Dim xl
Dim ol
Set xl = CreateObject("Excel.Application')
Set ol= GetObject(,"Outlook.Application')

...becomes...

require 'win32ole'
xl = WIN32OLE.new("Excel.Application")
ol = WIN32OLE.connect("Outlook.Application")

More VB-to-Ruby tips to follow. Let me know if there's anything in particular you'd like to see.