Add combobox into table cell

K

kdyjur

OK, I have a table where I click a button and it adds a row on the
bottom of the table. There are four columns in that row and I want to
place a combobox in column three of the new row.

I have looked everywhere and still cannot get the syntax 100% right:

Private Sub butAddTask_Click()
'1. Add Row
ActiveDocument.Tables(1).Rows.Add
'2. Count Rows
intNumberOfRowsTasks = (ActiveDocument.Tables(1).Rows.Count)
'3. Write Text into Cells
Tables(1).Cell(intNumberOfRowsMilestones, 3).Range.Text =
InlineShapes.AddOLEControl(ClassType:="Forms.ComboBox.1",Tables(1)(intNumberOfRowsMilestones,
3))

End Sub

Basically it's the third part of this that I want to get straight.
additionally I want to add four items to this new combobox. Can someone
PLEASE help???
 
H

Helmut Weber

Hi,

this is working for me here and now:

Sub Macro2()
Dim r As Long
With ActiveDocument.Tables(1)
.Rows.Add
r = .Rows.Count
.Cell(r, 3).Select
selection.Collapse
selection.InlineShapes.AddOLEControl ClassType:="Forms.ComboBox.1"
With selection.InlineShapes(1).OLEFormat.Object
.AddItem "0001"
.AddItem "0002"
.AddItem "0003"
End With
End With
End Sub

Might be possible do avoid the selection-object,
but I think it isn't relevant here.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Doug Robbins - Word MVP

This uses the Range object rather than the Selection object

Dim r As Range
With ActiveDocument.Tables(1)
.Rows.Add
Set r = .Cell(.Rows.Count, 3).Range
ActiveDocument.InlineShapes.AddOLEControl ClassType:="Forms.ComboBox.1",
Range:=r
With r.InlineShapes(1).OLEFormat.Object
.AddItem "0001"
.AddItem "0002"
.AddItem "0003"
End With
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

kdyjur

Both methods work equally as well. Thank-you gentlemen!! Would it
surprise you to know that I've been trying to come up with that on my
own for three days now? I'm new to VBA, so this is really a help.

I have this sub in there as well which I would like to fire for
"Combobox1", but other combobox's as well. Is there a way to make it
portable to new combobox's?

Private Sub ComboBox1_Change()
If ComboBox1.Text = "Addressed" Or ComboBox1.Text = "No Flag" Then
ComboBox1.BackColor = RGB(184, 225, 127)
ComboBox1.ForeColor = RGB(0, 0, 0)
ElseIf ComboBox1.Text = "Follow-Up" Then
ComboBox1.BackColor = RGB(73, 22, 109)
ComboBox1.ForeColor = RGB(255, 255, 255)
ElseIf ComboBox1.Text = "Waiting" Then
ComboBox1.BackColor = RGB(250, 202, 0)
ComboBox1.ForeColor = RGB(0, 0, 0)
End If
End Sub
 
H

Helmut Weber

Hi,
I have this sub in there as well which I would like to fire for
"Combobox1", but other combobox's as well. Is there a way to make it
portable to new combobox's?

writing code which manipulates code is possible, in principle.
You need a reference to the appropriate library.
Which was
microsoft visual basic for applications extensibility library 6.0,
or so, seemably in other versions than the one I got at present,
because I can't it anymore.

But it's hell, I'd say.

I did some experiments with it years ago.
But as it was to me of little practical use,
I forgot about it.

Another way would be, to use a selection-change event
and loop over all inlineshapes of
OLEFormat.ClassType = "Forms.ComboBox.1".

But it would terribly slow down editing. probably.
Maybe feasable for 1 page documents.

Or you mean something like that:

Private Sub ComboBox1_Change()
Combo
End Sub

Private Sub Combo()
Dim oCtrl As InlineShape
For Each oCtrl In ActiveDocument.InlineShapes
If oCtrl.OLEFormat.ClassType = "Forms.ComboBox.1" Then
If oCtrl.OLEFormat.Object.Text = "0001" Then
oCtrl.OLEFormat.Object.BackColor = RGB(184, 225, 127)
oCtrl.OLEFormat.Object.ForeColor = RGB(0, 0, 0)
End If
' ... more conditions and actions
End If
Next
End Sub

I'm at the end of my wisdom, sorry.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

Ted Williams

Thank you Helmut, this was actually greatly helpful. I just added a
command button to the form and when it is clicked, your sub is called.
It takes all of the combos and evaluates the text and colours the
accordingly.

Thanks again for taking the time to write good responses. I have
learned! Greetings from Canada.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top