List box issues

I am creating a word template that contains two list boxes. The user will be able to select multiple items and send the items to the other list box via a command button. However when ever I save and close the document and open the same document, the contents of the second list box are empty

I have attached the code I have for you all to look at. Thanks for the help in advance.

Private Sub Document_New()
PopulateListBox

'Clears out second list for a new document
lstbox2.Clear
End Sub

Private Sub Document_Open()
PopulateListBoxes
End Sub

Private Sub cmdMovetoRight_Click()

Dim i As Integer

If lstbox1.ListIndex = -1 Then Exit Sub
For i = lstbox1.ListCount - 1 To 0 Step -1
If lstbox1.Selected(i) = True Then
lstbox2.AddItem lstbox1.List(i)
lstbox1.RemoveItem i
End If
Next i
End Sub

Private Sub cmdMovetoLeft_Click()

Dim i As Integer

If lstbox2.ListIndex = -1 Then Exit Sub
For i = lstbox2.ListCount - 1 To 0 Step -1
If lstbox2.Selected(i) = True Then
lstbox1.AddItem lstbox2.List(i)
lstbox2.RemoveItem i
End If
Next i
End Sub

Private Function PopulateListBoxes()
'code used to populate multi-select list box
With lstbox1
.AddItem "TBD1"
.AddItem "TBD2"
.AddItem "TBD3"
.AddItem "TBD4"
.AddItem "TBD5"
.AddItem "TBD6"
.AddItem "TBD7"
End With
End Function


Answer this question

List box issues

  • lissbpp

    Hi

    Yeah that will happen, when your document opens both list boxes are empty. Your populating list box 1. You need to populate list box 2, which means when your document closes you need to store what items are in each listbox and when your document opens load the items that were set before and display them.

    You need to do this yourself as the data in the list boxes are not part of the document.

     



  • Scott Croisdale

    Wow....if I can get it to work that will be great. Do you think you could send me some sample code on how I might access the array in the Document.CustomDocumentProperties, or do you know where I can access that information My vb skills are not on that level to where I can fully understand the concept of this w/o having some code to look at. Thanks again for the help.

  • Lunarpc2

    Hi,

    I'd say you'd want to save the data with the document so that when it opens you can read it, maybe into an array, and set the list boxes. Have a look at Document.CustomDocumentProperties collection, use the array to hold the list box contents, and then save it with the document. When the documents open get the array and set up the list boxes.



  • giko

    Oh ok thanks.....how would you suggest that I do that I thought of saving the items to an array and then populating the items stored in the array when the document is opened. Would that work

  • List box issues