Consulting

Results 1 to 3 of 3

Thread: Comboboxes in Word Document

  1. #1
    VBAX Regular
    Joined
    Aug 2016
    Posts
    11
    Location

    Comboboxes in Word Document

    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...


    [VBA]
    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
    [/VBA]

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Your code makes me think that you have ActiveX comboboxes in the document. Is that true?
    Greg

    Visit my website: http://gregmaxey.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •