PDA

View Full Version : Listbox code...



emsa
08-30-2006, 09:48 AM
Sorry me again. I was kindly given some code (see below) to experiment with and it was pretty good seeing the results come up in the message box (little things!). Unfortunately, I require the results to be entered onto the word form itself. The vba form also loads when I open the document template but I need it to open only when a user is completing the form and gets to a certain formfield if that's at all possible. If anybody can be of assistance, I would be eternally grateful.


Private Sub CommandButton1_Click()
Dim counter As Integer

'loop through the list
For counter = 0 To ListBox1.ListCount - 1
'check if the item is selected
If ListBox1.Selected(counter) Then
'add here your code with what to do with the selected item
'as example shows a msgbox with the selected value

MsgBox ListBox1.List(counter)
End If
Next

End Sub
Private Sub Document_Open()
'When the document is opened add the different options
ListBox1.AddItem ("cs")
ListBox1.AddItem ("da")
ListBox1.AddItem ("de")
ListBox1.AddItem ("et")
ListBox1.AddItem ("el")
ListBox1.AddItem ("en")
ListBox1.AddItem ("es")
ListBox1.AddItem ("fr")
ListBox1.AddItem ("it")
ListBox1.AddItem ("lv")
ListBox1.AddItem ("lt")
ListBox1.AddItem ("hu")
ListBox1.AddItem ("mt")
ListBox1.AddItem ("nl")
ListBox1.AddItem ("pl")
ListBox1.AddItem ("pt")
ListBox1.AddItem ("sk")
ListBox1.AddItem ("sl")
ListBox1.AddItem ("fi")
ListBox1.AddItem ("sv")
End Sub

fumei
08-30-2006, 02:31 PM
I have been playing with this.

1. Do not use the Document_Open to load the listbox. Use Document_Open to call another procedure.Sub Document_Open()
Call LoadMyList
End Sub

Sub LoadMyList()
ListBox1.Clear
' whatever etc etc
ListBox1.AddItem ("sl")
ListBox1.AddItem ("fi")
ListBox1.AddItem ("sv")
' etc etc
End Sub
This allows you to clear and load the listbox ON YOUR CHOICE. If a user selects some items, you may (or...you may not) want to clear that selection. If you do not, then they will remain selected. You can clear and RE-load by simply calling LoadMyList.

I have been flitting with this off and on while doing something else. I am having a wee trouble getting it to work properly. I will look at it later, but if someone wants to take a whack at it, go ahead. I am using ListBox_LostFocus (although I have renamed the listbox lstSource) - lstSource_LostFocus.

Now, LostFocus builds an array of the selected items - that's fine, then it fires a Sub DoSomething. Oh...heck, here is the code.Private Sub lstSource_LostFocus()
Dim var
Dim i As Integer
On Error Resume Next
For var = 0 To lstSource.ListCount
If lstSource.Selected(var) <> False Then
ReDim Preserve SourceLanguages(i)
SourceLanguages(i) = lstSource.List(var)
' NOTE! If I have a message boc displaying
' what the selected item is, it displays it
' then displays all of them AGAIN
' ie. this procedure is fired TWICE
' see DoSomething for further note
i = i + 1
iCount = iCount + 1
End If
Next var
Call DoSomething
Call LoadList
End Sub

Sub DoSomething()
Dim var
Selection.EndKey unit:=wdStory
' This fired lstSource_LostFocus a SECOND time!
' why????
For var = 0 To UBound(SourceLanguages())
Selection.TypeText Text:=SourceLanguages(var) & vbCrLf
Next
Selection.Collapse Direction:=wdCollapseEnd
End Sub

NOTE: the array Public SourceLanguages() is of course declared in a module. The other stuff is run from ThisDocument.

fumei
08-30-2006, 02:34 PM
Crap, forgot to attach the file. Someone can go play with this...I have to do something else right now.

emsa
08-30-2006, 03:21 PM
Thanks Fumei for your time. I'm tryng to attach the word template but It will not allow me to attach files for some reason.

emsa
08-30-2006, 03:35 PM
The template contains a drop down formfield namely, "Target Language" which will only allow one item to be selected. I would like to be able to select multiple items, even all items and for them somehow to be placed on the form. It would be nice for the vba form to load when a user tabs to this field or bookmark, obviously the current form field will not be required. Many thanks.

fumei
08-31-2006, 12:20 AM
Again: " for them somehow to be placed on the form" MUST be defined! WHERE??????

emsa
08-31-2006, 01:11 AM
I would like the items once selected to be inserted into the table cell adjacent to "Target Langauge". Sorry if I'm not making myself clear.