PDA

View Full Version : Comboboxes in Word Document



GRCNC
08-24-2016, 11:40 AM
Hi - I'm a novice to VBA with Microsoft Word and am having trouble working with ComboBoxes and would like some help. I have a document with six ComboBoxes. The first ComboBox (Combobox1) is a drop down (1-5). The other ComboBoxes are named cb_LOC1, cb_LOC2, cb_LOC3, cb_LOC4, and cb_LOC5. Whatever number is selected, I'd like to "show" that number of ComboBoxes and "hide" the rest. My code is below and I'm receiving an error (Compile error: invalid qualifier) at the "i" in cb_LOC& i. FYI: I'm using Width and Height b/c I can't find the "Visible" property.

Does anyone have any suggestions as to what I'm doing wrong.

Finally, not sure if the code below is going to come over in code tags or not. I read the rules and it indicates there should be a "VBA" button up top, but I don't see it. Apologies in advance...



Dim cb As ComboBox
Dim i As Long
For i = ComboBox1.Value + 1 To 5

cb_LOC& i.Width = 1
cb_LOC& i.Height = 1

next i

gmayor
08-29-2016, 05:11 AM
I missed this earlier, but the solution is simple enough

Option Explicit

Private Sub ComboBox1_Change()
Dim oCtrl As Control
Dim i As Long
Dim lngNum As Long: lngNum = ComboBox1.Value
For Each oCtrl In Me.Controls
Select Case lngNum
Case 0
If oCtrl.Name Like "cb_LOC*" Then
oCtrl.Visible = False
End If
Case Else
If oCtrl.Name Like "cb_LOC*" Then
oCtrl.Visible = False
End If
For i = 1 To lngNum
If oCtrl.Name = "cb_LOC" & i Then
oCtrl.Visible = True
End If
Next i
End Select
Next oCtrl
End Sub

Private Sub UserForm_Initialize()
Dim i As Long
With ComboBox1
For i = 0 To 5
.AddItem i
Next i
.ListIndex = 0
End With
End Sub

gmaxey
08-29-2016, 01:09 PM
Your code makes me think that you have ActiveX comboboxes in the document. Is that true?