Special action required for one item in a listbox

Can anybody please help with the folowing request,

To add an item "other" to the array (the easy bit) and when a user selects this item, an input box pops up and allows the use to type in text and then for it to get listed in the same cell location. Many thanks for any assistance.

Option Explicit

Private Sub CommandButton1_Click()
Dim i As Integer
Dim s As String
'ActiveDocument.ListBox1.Clear
s = ""
For i = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(i) = True Then
s = s & Me.ListBox1.List(i) & ", "
End If
Next i
'
If Len(s) > 0 Then
s = Mid(s, 1, Len(s) - 2)
ActiveDocument.Unprotect
ActiveDocument.Tables(1).Cell(9, 2).Range.InsertAfter (s)
ActiveDocument.Protect (wdAllowOnlyFormFields)
Else
ClearCell
End If

Unload Me
End Sub

Private Sub CommandButton2_Click()
ClearCell
End Sub

Private Sub UserForm_Initialize()
Dim vArray As Variant

vArray = Array("cs", "da", "de", "et", "el", "en", "es", "fr", "it", "lv", "lt", "hu", "mt", "nl", "pl", "pt", "sk", "sl", "fi", "sv", "other")
Me.ListBox1.List = vArray

End Sub

Private Function ClearCell()
ActiveDocument.Unprotect
ActiveDocument.Tables(1).Cell(9, 2).Range.Delete
ActiveDocument.Protect (wdAllowOnlyFormFields)
End Function




Answer this question

Special action required for one item in a listbox

  • KrazyKevin

    Hi,

    In this line

    s = s & Me.ListBox1.List(i) & ", "

    If you remove the & ", " from the end of the statment then you won't need to this line

    s = Mid(s, 1, Len(s) - 2)

    as thats removing the ", " you appended.

    To add in the input box that appears if other is selected....

    If Len(s) > 0 Then

    If s = "other" Then
    s = InputBox("Enter new text: ") 'replaces 'other' with inputted text
    End If

    ActiveDocument.Unprotect
    ActiveDocument.Tables(1).Cell(9, 2).Range.InsertAfter (s)
    ActiveDocument.Protect (wdAllowOnlyFormFields)
    Else
    ClearCell
    End If



  • Special action required for one item in a listbox