Results 1 to 4 of 4
  1. #1
    Lounger
    Join Date
    Dec 2001
    Location
    Santa Barbara, California, USA
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Control Arrays in VBA? (Word 2000/2002)

    It appears that unlike VB, VBA for Word does not support control arrays.
    I've got four checkboxes in each of 4 frames on a form, with the same tests needed for each set of 4 checkboxes. I've tried creating the checkbox names using strings concatenated with numeric indexes, but I haven't yet found a what to turn the resulting strings into variable or checkbox names. Is there any solution for this, some string to variable name conversion?

    Thanks,
    Steve

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Control Arrays in VBA? (Word 2000/2002)

    UserForms in Microsoft Office are different from VB6 forms, as you found out. You can use the Controls property of a UserForm to refer to a control by its name as a string. Let's say that you have check boxes named CheckBox1 to CheckBox4 on a form named UserForm1.

    Dim i As Integer
    For i = 1 To 4
    MsgBox UserForm1.Controls("CheckBox" & i).Value
    Next i

    Note : if this code is run on the form itself, you can use Me.Controls instead of UserForm1.Controls.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Control Arrays in VBA? (Word 2000/2002)

    If there's nothing else in the frames, you could use the Frame's Controls collection. I played with 2 frames with 2 checkboxes each and this code, and it seems to work:

    Private Sub UserForm_Click()
    Dim ctrl As MSForms.Control
    For Each ctrl In Me.Frame1.Controls
    Debug.Print ctrl.Name, ctrl.Value
    Next
    For Each ctrl In Me.Frame2.Controls
    Debug.Print ctrl.Name, ctrl.Value
    Next
    End Sub

    I'm not sure you can assume that the order will be anything in particular, 'though I think "order added" is the most likely.

  4. #4
    Lounger
    Join Date
    Dec 2001
    Location
    Santa Barbara, California, USA
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Control Arrays in VBA? (Word 2000/2002)

    Thank You, that's just what I need.

    Steve

Posting Permissions

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