Consulting

Results 1 to 5 of 5

Thread: Word VBA UserForm Array ListBox selections to FormField

  1. #1
    VBAX Regular
    Joined
    Jun 2016
    Posts
    53
    Location

    Word VBA UserForm Array ListBox selections to FormField

    Hello,

    i am trying to populate a formfield with items from a listbox which is populated via an array.

    This is what i have so far:
    Private Sub UserForm_Initialize()
    Dim arrData As Variant
    arrData = Array("Entry1", "Entry2", "Entry3", "Entry4", _
                    "Entry5", "Entry6")
    ListBox1.List = arrData
    End Sub
    
    Public Sub cmdOK_Click()
    Dim intListBox As Integer
    Dim intAusgabe As Integer
            
          For intListBox = 0 To ListBox1.ListCount - 1
              If ListBox1.Selected(intListBox) Then
              intAusgabe = intListBox + 1
                ActiveDocument.FormFields("Hinweise").Result = ListBox1.List(intAusgabe) & Chr(11)
              End If
          Next intListBox
        Hide
    lbl_Exit:
        Exit Sub
      End Sub
    
    'Empty FormField Button
    Private Sub cmdEmpty_Click()
        ActiveDocument.FormFields("Hinweise").Result = ""
        Hide
    lbl_Exit:
        Exit Sub
    End Sub
    
    'Cancel Button
    Private Sub cmdESC_Click()
      Unload Me
    End Sub
    The Problem is that when i select multiple items in the Listbox, only the last selected item gets transfered to the formfield.
    (Item 1,2,4,6 selected, only item 6 is transfered etc.)

    Does anyone know why this is happening and how to correct this issue?
    Although the Items need to be written in a new line for each item. vbNewLine Does not work, is there an alternative?


    Best Regards

    Manuel

  2. #2
    The problem is that you are replacing each selection with the next what you need is

    Public Sub cmdOK_Click()
    Dim intListBox As Integer
    Dim strText As String
    
        For intListBox = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(intListBox) Then
                If strText = "" Then
                    strText = ListBox1.List(intListBox)
                Else
                    strText = strText & Chr(11) & ListBox1.List(intListBox)
                End If
            End If
        Next intListBox
        ActiveDocument.FormFields("Hinweise").Result = strText
        Hide
    lbl_Exit:
        Exit Sub
    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
    VBAX Regular
    Joined
    Jun 2016
    Posts
    53
    Location
    All right, your modification is working fine.
    I see where i made my mistake. =)
    Thanks for helping out!

  4. #4
    VBAX Regular
    Joined
    Jun 2016
    Posts
    53
    Location
    Ok i ran into another issue after playing with the code.
    I tried to complexify it further because i need additional functionality.

    Let me try to explain it:
    When i select the string entry of the listbox, i want that entry to be converted to a longer string, because i don't wnat to clutter the listboxes with a wall of text.

    For example:
    I select Entry 1 "Gasket" - then ultimately what ends up in the Formfield is text like "PROTEGO DR/U-50 PTFE housing gasket replaced".

    Therefore i had to declare another variable to initialize it with this code, it grabs additional information from other formfields.
    But when i now want to select more than one entry, only the first one gets converted, the second, third etc. selection only results in the short text example.

    I don't know if it makes sense to you, i will post the code that i modified:
    Public Sub cmdOK_Click()
    
    Dim intListBox As Integer
    Dim strText As String
    Dim secondaryText As String 'additional variable that stores the long string 
    
        For intListBox = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(intListBox) Then
                If strText = "" Then
                    strText = ListBox1.List(intListBox)
    
                    'additional if statement to check which listbox entry is selected and what text to store in the additional variable
                    If strText = "Gehäusedichtung" Then
                    secondaryText = "• " & ActiveDocument.FormFields("Hersteller").Range & "®" & " " & ActiveDocument.FormFields("Typ").Range & _
                    "-" & ActiveDocument.FormFields("TypAdd1").Result & "-" & ActiveDocument.FormFields("TypAdd2").Result & _
                    " " & ActiveDocument.FormFields("GDTNG").Range & " " & "Gehäusedichtung erneuert."
                    End If
                    
                    If strText = "2x Gehäusedichtung" Then
                    secondaryText = "• " & "2x " & ActiveDocument.FormFields("Hersteller").Range & "®" & " " & ActiveDocument.FormFields("Typ").Range & _
                    "-" & ActiveDocument.FormFields("TypAdd1").Result & "-" & ActiveDocument.FormFields("TypAdd2").Result & _
                    " " & ActiveDocument.FormFields("GDTNG").Range & " " & "Gehäusedichtung erneuert."
                    End If
                    
                Else
                    secondaryText = secondaryText & Chr(11) & ListBox1.List(intListBox)
                End If
            End If
        Next intListBox
        ActiveDocument.FormFields("Hinweise").Result = secondaryText
        Hide
    lbl_Exit:
        Exit Sub
    End Sub
    I think a For Loop may not be the right type of loop for this task. I see no way to convert the second, third etc. selected listbox entries to a variable in order to properly populate the formfield if more than one listbox entries need to be converted to its long text versions.

    Do you have an idea how this can be achieved?


    Best Regards

    Manuel

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Public Sub cmdOK_Click()
    Dim lngIndex As Long
    Dim strText As String
    Dim strSecondaryText As String 'additional variable that stores the long string
      strSecondaryText = vbNullString
      For lngIndex = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(lngIndex) Then
          strText = vbNullString
          strText = ListBox1.List(lngIndex)
          If Len(strSecondaryText) = 0 Then
            If strText = "Gehäusedichtung" Then strSecondaryText = "• Some long string of text."
            If strText = "2x Gehäusedichtung" Then strSecondaryText = "• Some other long string of text."
          Else
            If strText = "Gehäusedichtung" Then strSecondaryText = strSecondaryText & Chr(11) & "• Some long string of text."
            If strText = "2x Gehäusedichtung" Then strSecondaryText = strSecondaryText & Chr(11) & "• Some other long string of text."
          End If
        End If
      Next lngIndex
      MsgBox strSecondaryText
      Hide
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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