PDA

View Full Version : ComboBox in a Word Table Cell



JonBattle
06-15-2008, 05:37 PM
Im new at VB and cant get this to work. I want a ComboBox in a Table cell that has values of "Rain" "Snow" "Sleet" - I want user to pick one...value goes nowhere as document is to be printed.

In another cell....same thing...different values "Hot" "Sunny" "Warm"

Ive done the "Insert Combo Box", View Code etc and tried some different things.....to no avail...

One thing that keeps happening is that one box takes on the dropdown values of the other....

Appreciate any help...JonB

pnewton
06-15-2008, 07:13 PM
Have you tried using forms?

JonBattle
06-15-2008, 10:09 PM
Im trying to put this drop down into a cell in a Word Table...so am not sure how forms will help. Im using Word 2007 in (2003) Compatibility Mode

CreganTur
06-16-2008, 06:31 AM
I think what pnewton meant is that you could create a userform where your users can select all of the options at once. Then you could have a button on the form that would insert the chosen values into the letter.

One easy way to do this would be to inert a uniquely named bookmark at each place where you want a chosen value entered. Then you could use
ActiveDocument.Bookmarks(Name).Range.Text = Value
Where Name would be the name of the bookmark you want to add the value to, and Value is a variable that represents the chosen value of the combo box on the userform.

pnewton
06-16-2008, 01:10 PM
Try this:

Private Sub ComboBox1_GotFocus()
Dim lngAnswer As Long
With ComboBox1
'Save Current selection
lngAnswer = .ListIndex
'Remove items or we will keep adding them over an over...
.Clear
'Add the items
.AddItem "Hot"
.AddItem "Sunny"
.AddItem "Warm"
.AddItem "Cold"
'Reset the selection
.ListIndex = lngAnswer
End With
End Sub


As long as you have code for each combobox you should be fine.

Cheers

P

CreganTur
06-16-2008, 01:19 PM
As long as you have code for each combobox you should be fine.


Also be sure that your combo boxes have unique names. Using the default names can lead to some issues, and it's not good practice to use them either.

fumei
06-17-2008, 09:41 AM
The assumption is that these are ActiveX controls, which from the original post is a fair assumption.

Nevertheless, as pnewton mentions (although with all due respect, not fully), the other possibility is to use formfields.

pnewton, stating
"Have you tried using forms", is not clear. First of all, the term "forms" is not explicit. I think you meant formfields, but you could also have meant userforms. Do see what I mean? It is good to be clear and explicit.

Demo attached with selectable choices using formfields, not ActiveX controls. You certainly could use ActiveX controls (a ComboBox). Note however, that ActiveX controls are affected by the security settings in Word. Formfields are not.

fumei
06-17-2008, 09:46 AM
Oh, and Randy is very correct re: unique names for your controls (if you use them). As a general best-practice, it is better to never keep the default names. I never do. I always change the names of all controls, even Labels and frames on userforms.

Frame1 (for example) is essentially meaningless.

It is not a big deal if you only have a couple of controls, but explicit naming is a VERY good habit to get into, as at some point you may have a large project.

fumei
06-17-2008, 09:54 AM
pnewton: have you actually used the code for your combobox, as posted?

1. You use GotFocus. You are making an assumption the control already has items. It may, or it may not. If not, then .ListIndex = -1...nothing. If yes, it is STILL -1, nothing.

2. the display in the combobox is empty (blank)

3. the selected item in an ActiveX control persists, so I am unclear as to what the purpose is, anyway.

4. why are you even clearing it, and repopulating????

I am not being critical, I am being curious.