# Ruby as excel vba event handler replacement using win32ole?

**URL:** <https://www.ruby-forum.com/t/ruby-as-excel-vba-event-handler-replacement-using-win32ole/52305>\
**Category:** Ruby\
**Created:** [January 12, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/ruby-as-excel-vba-event-handler-replacement-using-win32ole/52305 "2006-01-12T18:16:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ammonc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@ammonc](https://www.ruby-forum.com/u/ammonc)\
**Post date:** [January 12, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/ruby-as-excel-vba-event-handler-replacement-using-win32ole/52305/1 "2006-01-12T18:16:50Z")

</div>

I’ve seen Ruby used as a script to save all Word doc files as rtf  
instead.  
Can ruby be used interactively to process events that would usually be  
handled in VBA in Excel for example?

---

<div class="post-metadata">

**Author:** ![ammonc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@ammonc](https://www.ruby-forum.com/u/ammonc)\
**Post date:** [January 16, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/ruby-as-excel-vba-event-handler-replacement-using-win32ole/52305/2 "2006-01-16T12:47:41Z")

</div>

Hello,  
In message “ruby as excel vba event handler replacement using win32ole?”  
on 06/01/13, Ammon C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

> Can ruby be used interactively to process events that would usually be  
> handled in VBA in Excel for example?

Does WIN32OLE\_EVENT class help you?

require ‘win32ole’

def sheet\_changed(arg1, arg2)  
p arg1.value  
end

excel = WIN32OLE.new(“Excel.Application”)  
excel.visible = true  
book = excel.workbooks.Add  
ev = WIN32OLE\_EVENT.new(book, ‘WorkbookEvents’)  
ev.on\_event(‘SheetChange’){|arg1, arg2|  
sheet\_changed(arg1, arg2)  
}

book.Worksheets(1).Range(“A1”).value = 100  
book.Worksheets(1).Range(“A2”).value = 200  
book.Worksheets(1).Range(“B1”).value = 300

---

<div class="post-metadata">

**Author:** ![ammonc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/58f4c7/32.png) [@ammonc](https://www.ruby-forum.com/u/ammonc)\
**Post date:** [January 17, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/ruby-as-excel-vba-event-handler-replacement-using-win32ole/52305/3 "2006-01-17T19:07:34Z")

</div>

Thanks, I’ll try that! That is the kind of thing I was looking for -  
event  
handling in Ruby.
