Adding a drop-down programmatically

M

Miked

I'd like to be able to add 2 drop downs to 2 cells of a table in word,
but I'd like to do it programmatically. I'd like to be able to do this
off of the add-row event in word (the event that adds a table row); Is
this possible, and if so, can you provide direction?

Thank you very much in advance!

MikeD
 
Z

zkid

If you are talking about inserting an ActiveX drop down box (which is
actually a combo box):

(Just did this off of macro recording with the Contol toobox toolbar open)

ActiveDocument.ToggleFormsDesign
Selection.InlineShapes.AddOLEControl ClassType:="Forms.ComboBox.1"

As far as inserting the data, well, that's beyond my scope. Maybe someone
else can help. Helmut? You are awesome at this stuff....

You can also insert a drop down if you are using protected forms:

You'd have to unprotect the form first, of course...

Selection.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormDropDown

You can load this up (provide data) programmically by using its bookmark
name (you can assign your own). If you aren't using a protected form, however

Sample from VBA help:

With Documents("Form.doc").FormFields("Places") .DropDown.ListEntries
.Add Name:="Seattle"
End With

Then re-protect the form without re-setting it.

Greetings from California - zkid
 
J

Jean-Guy Marcil

Miked was telling us:
Miked nous racontait que :
I'd like to be able to add 2 drop downs to 2 cells of a table in word,
but I'd like to do it programmatically. I'd like to be able to do
this off of the add-row event in word (the event that adds a table
row); Is this possible, and if so, can you provide direction?

Thank you very much in advance!

If you mean ActiveX ComboBoxes, then try this code:

Dim TargetTable As Table
Dim myCtrl As InlineShape

Set TargetTable = ActiveDocument.Tables(1)

With TargetTable
.Rows.Add

With .Rows(TargetTable.Rows.Count - 1)
Set myCtrl = .Cells(1).Range.InlineShapes.AddOLEControl _
(ClassType:="Forms.ComboBox.1")
With myCtrl.OLEFormat.Object
.Style = 2 ' fmStyleDropDownList
.AddItem "123"
.AddItem "456"
.AddItem "789"
.AddItem "012"
End With

Set myCtrl = .Cells(2).Range.InlineShapes.AddOLEControl _
(ClassType:="Forms.ComboBox.1")
With myCtrl.OLEFormat.Object
.Style = 2 ' fmStyleDropDownList
.AddItem "abc"
.AddItem "def"
.AddItem "ghi"
.AddItem "jkl"
End With

End With

End With


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Jean-Guy Marcil was telling us:
Jean-Guy Marcil nous racontait que :
Miked was telling us:
Miked nous racontait que :


If you mean ActiveX ComboBoxes, then try this code:

Dim TargetTable As Table
Dim myCtrl As InlineShape

Set TargetTable = ActiveDocument.Tables(1)

With TargetTable
.Rows.Add

With .Rows(TargetTable.Rows.Count - 1)
Set myCtrl = .Cells(1).Range.InlineShapes.AddOLEControl _
(ClassType:="Forms.ComboBox.1")
With myCtrl.OLEFormat.Object
.Style = 2 ' fmStyleDropDownList
.AddItem "123"
.AddItem "456"
.AddItem "789"
.AddItem "012"
End With

Set myCtrl = .Cells(2).Range.InlineShapes.AddOLEControl _
(ClassType:="Forms.ComboBox.1")
With myCtrl.OLEFormat.Object
.Style = 2 ' fmStyleDropDownList
.AddItem "abc"
.AddItem "def"
.AddItem "ghi"
.AddItem "jkl"
End With

End With

End With

Ooops... remove the "- 1" in the line
With .Rows(TargetTable.Rows.Count - 1)
so that it looks like:
With .Rows(TargetTable.Rows.Count)

Sorry about that.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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