PDA

View Full Version : Which items in a listbox are not selected?



clhare
11-01-2007, 06:08 AM
I have a multiselect listbox on a userform. Each item in the listbox refers to specific text in the document. How would I determine which items in the listbox are not selected so I can delete their associated text?

Any help is greatly appreciated!

Cheryl

OTWarrior
11-01-2007, 06:17 AM
what do you mean by a multiselect listbox? i thought you only select one option with a listbox?

clhare
11-01-2007, 06:32 AM
With this listbox I have the MultiSelect property set to "1 - fmMultiSelectMulti" so more than one item can be selected.

OTWarrior
11-01-2007, 08:56 AM
is there some kind of result property for the listbox so you can read the values? or would it use an index for each item in the list?

TonyJollans
11-01-2007, 09:59 AM
This should give you a start ...
For i = 0 To Me.ListBox1.ListCount - 1
If Not Me.ListBox1.Selected(i) Then MsgBox Me.ListBox1.List(i)
Next

clhare
11-02-2007, 05:16 AM
The message box just gives me each item in the listbox one at a time, whether I selected it or not.

Also, how would I run the steps that apply to each option in the list box if I'm using "i"?

TonyJollans
11-02-2007, 05:36 AM
I'm not sure what else to suggest - it ought to work and it does for me. Can you post a failing document?

And I'm not sure what you mean about steps to take. If you know the non-selected item you do whatever you want - "i" is just an index into the list so you can get the properties of an individual item.

clhare
11-02-2007, 06:41 AM
I created a test doc and the macro you gave above does work now (I must have done something wrong before) and the message box only shows the options not selected. But, I can't figure out to use that code to delete text associated with the specific options that are not selected.

TonyJollans
11-02-2007, 09:04 AM
This will do what you want in this case:

For i = 0 To Me.lstParagraphs.ListCount - 1
If Not Me.lstParagraphs.Selected(i) Then
BookmarkName = Replace(Me.lstParagraphs.List(i), " ", "")
ActiveDocument.Bookmarks(BookmarkName).Range.Delete
End If
Next


More generally you need to define how the bookmark name is derived from the list item and code as appropriate

clhare
11-02-2007, 10:19 AM
Thank you!!