preload
Correct file paths for WIN32OLE applications in Ruby Date Formatting in Excel using Ruby
Nov 26

There have been a couple of Ruby websites that  tell you how to import Excel Constant values into your program….but some seem to not be quite correct.   I don’t know if they are using a different version of Ruby or what, but here’s what has worked for me.

In your Ruby program, add the following lines:

require 'win32ole'

class ExcelConst
# blank method to hold all the constants
end
...

file = Dir.pwd + "/" + @YourFilenameInHere
efile = getAbsolutePathName(file)
xl = WIN32OLE.new('Excel.Application')
xl.visible = true
wb = xl.Workbooks.Open("#{efile}")
ws = wb.Worksheets(1)
ws.Select
# Load in Excel Constants
WIN32OLE.const_load(xl, ExcelConst)

Note that the ‘ExcelConst’ is class not a method or a def as I have seen on some other websites.  I tried those  and received this error

excel_example.rb:189: uninitialized constant ExcelConst (NameError)
Declaring them as a class gets around this error.
require ‘win32ole’
SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon

One Response to “Importing Excel Constants values into Ruby”

  1. Drawing Borders in Excell using Ruby Says:

    [...] Once the values are all loaded in, its time to make the spreadsheet look pretty. Here’s how you draw a single, continuous, thin line around each cell in a range of cells. This function can be used an any number of cells…but only in one row. If you give it a range of more than one row, then it will draw a box around the rows, and add the vertical lines. As I am adding one row at a time in my applications, my function works fine for single rows. The ExcelConst:: constants were address in a previous post. [...]

Leave a Reply