# OLE Object - combo box problem

**URL:** <https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856>\
**Category:** Ruby\
**Created:** [September 29, 2007, 9:17am UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856 "2007-09-29T09:17:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 29, 2007, 9:17am UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/1 "2007-09-29T09:17:21Z")

</div>

-i want to add to an excel worksheet a meniu accessing from a combo box  
but i have problems insering data in the combo box  
-in the vba i have combo.additem “opt1”  
-in ruby i cant use additem item (unknown property or method `additem’);  
what can i use instead?, i dont want to use fillinrange; and how can i  
go to another worksheet after i selected a option in the combo box

i have : $combo\_box=$worksheet\_menu.OLEObjects.Add “Forms.ComboBox.1”  
$combo\_box[‘additem’]=“opt1” doesnt work  
tnx

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 29, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/2 "2007-09-29T15:32:55Z")

</div>

Bulhac M. wrotes:

> i have : $combo\_box=$worksheet\_menu.OLEObjects.Add “Forms.ComboBox.1”  
> $combo\_box[‘additem’]=“opt1” doesnt work  
> tnx

Try $combo\_box.object.additem(“opt1”).

Regards,  
Masaki S.

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 29, 2007, 7:19pm UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/3 "2007-09-29T19:19:18Z")

</div>

tnx a lot, now additem its working, but for the combo box menu make it  
work (options to be links to another worksheet) i dont want tu use ruby,  
i want that with ruby script to record a macro and that macro is called  
when i select a option from the combo box because my ruby script is  
closed when i’m reading the excel file; is that possible?

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 29, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/4 "2007-09-29T16:39:26Z")

</div>

Hello,

Bulhac M. wrote:

> what can i use instead?, i dont want to use fillinrange; and how can i  
> go to another worksheet after i selected a option in the combo box

Sorry, I had not noticed your second question.

Try to use WIN32OLE\_EVENT and ‘MdcComboEvents’.

require ‘win32ole’  
ex = WIN32OLE.new(‘Excel.Application’)  
ex.visible = true  
book = ex.workbooks.add  
sheet = book.worksheets(1)  
combo = sheet.OLEObjects.Add(“Forms.Combobox.1”).object

book.worksheets.each do |sheet|  
combo.additem(sheet.name)  
end  
combo.additem(“exit ruby”)

ev = WIN32OLE\_EVENT.new(combo, ‘MdcComboEvents’)  
ev.on\_event(‘Change’){|\*arg|  
if combo.text == ‘exit ruby’  
$LOOP = false  
else  
book.worksheets(combo.text).activate  
end  
}  
$LOOP = true  
while $LOOP  
WIN32OLE\_EVENT.message\_loop  
sleep 0.1  
end

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 30, 2007, 12:11am UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/5 "2007-09-30T00:11:26Z")

</div>

Bulhac M. wrote:

> tnx a lot, now additem its working, but for the combo box menu make it  
> work (options to be links to another worksheet) i dont want tu use ruby,  
> i want that with ruby script to record a macro and that macro is called  
> when i select a option from the combo box because my ruby script is  
> closed when i’m reading the excel file; is that possible?

I’m not sure that I understand what you want to do,  
But does the following script help you?

require ‘win32ole’  
ex = WIN32OLE.new(‘Excel.Application’)  
ex.visible = true  
book = ex.workbooks.add  
sheet = book.worksheets(1)  
combo = sheet.OLEObjects.Add(“Forms.Combobox.1”).object  
oles = sheet.OLEObjects  
book.worksheets.each do |sh|  
combo.additem(sh.name)  
end  
modu = book.VBProject.VBComponents.Item(sheet.name)  
modu.CodeModule.InsertLines(1, "Private Sub " + combo.name + “\_Change”)  
modu.CodeModule.InsertLines(2, " MsgBox " + combo.name + “.Text” )  
modu.CodeModule.InsertLines(3, “End Sub”)

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 30, 2007, 9:19am UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/6 "2007-09-30T09:19:46Z")

</div>

Yes, this is what i want, tnx a lot, but it gives me this error:

OLE error code:800A03EC in Microsoft Office Excel  
Programmatic access to Visual Basic Project is not trusted

```
HRESULT error code:0x80020009
  Exception occurred. from excel2.rb:205:in `excel_asitt_menu'

```

from excel2.rb:349

my code is:

modu =  
$asitt\_workbook.VBProject.VBComponents.Item($worksheet\_asitt\_menu)  
modu.CodeModule.InsertLines(1, "Private Sub " + $combo\_box + “\_Change”)  
modu.CodeModule.InsertLines(2, " MsgBox " + $combo\_box + “.Text” )  
modu.CodeModule.InsertLines(3, “End Sub”)

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 30, 2007, 10:49am UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/7 "2007-09-30T10:49:09Z")

</div>

Hello,  
Bulhac M. wrote:

> previous error was resolved, but i still get one:
> 
> excel2.rb:207:in `method_missing': Item (WIN32OLERuntimeError) OLE error code:800A000D in VBAProject Type mismatch HRESULT error code:0x80020009 Exception occurred. from excel2.rb:207:in`excel\_asitt\_menu’
> 
> 207 line is :

modu=$asitt\_workbook.VBProject.VBComponents.Item($worksheet\_asitt\_menu)

Is $worksheet\_asitt\_menu WIN32OLE object?  
If so, try  
Item($worksheet\_asitt\_menu.name)

Item’s argument must be Worksheet name string(ie. String object)  
not WIN32OLE object.

Regards,  
Masaki S.

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 30, 2007, 9:30am UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/8 "2007-09-30T09:30:43Z")

</div>

Bulhac M. wrote:

> Yes, this is what i want, tnx a lot, but it gives me this error:
> 
> OLE error code:800A03EC in Microsoft Office Excel  
> Programmatic access to Visual Basic Project is not trusted
> 
> ```
> HRESULT error code:0x80020009
> Exception occurred. from excel2.rb:205:in `excel_asitt_menu'
> 
> ```
> 
> from excel2.rb:349
> 
> my code is:
> 
> modu =  
> $asitt\_workbook.VBProject.VBComponents.Item($worksheet\_asitt\_menu)  
> modu.CodeModule.InsertLines(1, "Private Sub " + $combo\_box + “\_Change”)  
> modu.CodeModule.InsertLines(2, " MsgBox " + $combo\_box + “.Text” )  
> modu.CodeModule.InsertLines(3, “End Sub”)

previous error was resolved, but i still get one:

excel2.rb:207:in `method_missing': Item (WIN32OLERuntimeError) OLE error code:800A000D in VBAProject Type mismatch HRESULT error code:0x80020009 Exception occurred. from excel2.rb:207:in`excel\_asitt\_menu’

207 line is :  
modu=$asitt\_workbook.VBProject.VBComponents.Item($worksheet\_asitt\_menu)

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 30, 2007, 11:16am UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/9 "2007-09-30T11:16:00Z")

</div>

Masaki S. wrote:

> Hello,  
> Bulhac M. wrote:
> 
> > previous error was resolved, but i still get one:
> > 
> > excel2.rb:207:in `method_missing': Item (WIN32OLERuntimeError) OLE error code:800A000D in VBAProject Type mismatch HRESULT error code:0x80020009 Exception occurred. from excel2.rb:207:in`excel\_asitt\_menu’
> > 
> > 207 line is :
> 
> modu=$asitt\_workbook.VBProject.VBComponents.Item($worksheet\_asitt\_menu)
> 
> Is $worksheet\_asitt\_menu WIN32OLE object?  
> If so, try  
> Item($worksheet\_asitt\_menu.name)
> 
> Item’s argument must be Worksheet name string(ie. String object)  
> not WIN32OLE object.
> 
> Regards,  
> Masaki S.

i have this:

$excel = WIN32OLE.new(‘Excel.Application’)  
$worksheet\_asitt\_menu = $asitt\_workbook.Worksheets(1)  
$worksheet\_asitt\_menu.name=“ASITT\_MENU”

$combo\_box=$worksheet\_asitt\_menu.OLEObjects.Add(“Forms.Combobox.1”).object

modu =  
$asitt\_workbook.VBProject.VBComponents.Item($worksheet\_asitt\_menu.name)  
modu.CodeModule.InsertLines(1, “Private Sub ComboBox1\_Change”)  
modu.CodeModule.InsertLines(2, " MsgBox “asdasd”" )  
modu.CodeModule.InsertLines(3, “End Sub”)

-combobox1 is the name for combo box

with this code i still have that error:

excel2.rb:208:in `method_missing': Item (WIN32OLERuntimeError) OLE error code:800A0009 in VBAProject Subscript out of range HRESULT error code:0x80020009 Exception occurred. from excel2.rb:208:in`excel\_asitt\_menu’  
from excel2.rb:352

#line 208:  
modu =  
$asitt\_workbook.VBProject.VBComponents.Item($worksheet\_asitt\_menu.name)

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 30, 2007, 12:26pm UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/10 "2007-09-30T12:26:58Z")

</div>

TNX a lot man, its working now; indeed it was with that comp.name a  
problem, tnx again

---

<div class="post-metadata">

**Author:** ![mihai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mihai](https://www.ruby-forum.com/u/mihai)\
**Post date:** [September 30, 2007, 12:21pm UTC](https://www.ruby-forum.com/t/ole-object-combo-box-problem/115856/11 "2007-09-30T12:21:35Z")

</div>

Bulhac M. wrote:

> i have this:
> 
> $excel = WIN32OLE.new(‘Excel.Application’)  
> $worksheet\_asitt\_menu = $asitt\_workbook.Worksheets(1)  
> $worksheet\_asitt\_menu.name=“ASITT\_MENU”

$combo\_box=$worksheet\_asitt\_menu.OLEObjects.Add(“Forms.Combobox.1”).object

> modu =

$asitt\_workbook.VBProject.VBComponents.Item($worksheet\_asitt\_menu.name)

> modu.CodeModule.InsertLines(1, “Private Sub ComboBox1\_Change”)  
> modu.CodeModule.InsertLines(2, " MsgBox “asdasd”" )  
> modu.CodeModule.InsertLines(3, “End Sub”)
> 
> -combobox1 is the name for combo box
> 
> with this code i still have that error:
> 
> excel2.rb:208:in `method_missing': Item (WIN32OLERuntimeError) OLE error code:800A0009 in VBAProject Subscript out of range HRESULT error code:0x80020009 Exception occurred. from excel2.rb:208:in`excel\_asitt\_menu’  
> from excel2.rb:352
> 
> #line 208:  
> modu =

$asitt\_workbook.VBProject.VBComponents.Item($worksheet\_asitt\_menu.name)

That’s because you have changed $worksheet\_asitt\_menu.name to  
‘ASITT\_MENU’,  
and you don’t specify correct VBComponent name as Item argument.  
(The error message is ‘Subscript out of range’.)

At first, check available name by using following code  
before line 208.  
$asitt\_workbook.VBProject.VBComponents.each do |comp|  
puts comp.name  
end

Or try to invoke VBE of Excel and check available names.  
If you could see ‘Sheet1(ASITT\_MENU)’ in VBAProject, then  
specify ‘Sheet1’ instead of $worksheet\_asitt\_menu.name

modu =  
$asitt\_workbook.VBProject.VBComponents.Item(‘Sheet1’)

Regards,  
Masaki S.
