PDA

View Full Version : Populating a ListBox row in a form with the results of a search for long sentences



Jake Johnson
08-12-2012, 09:27 AM
I'm now taking the next step and wanting to use a list of first words in long sentences to populate a column in a user form's listbox.

The following code seems as though it should work but it returns the error message "Could not set the RowSource property. Invalid property value." Not sure where I've gone wrong. The required value for a RowSource is a string, and I'm sending a string to it. Suggestions?


Sub FindStartOfLongSentenesAndPutInUserForm()
'This should list the first word of each sentence longer than 10 words in a user form Listbox

Dim asentence As Range
Dim str As String
For Each asentence In ActiveDocument.Range.Sentences
If asentence.Words.Count > 10 Then
str = str & asentence.Words(1)
End If
Next

With UserForm1
.ListBox1.RowSource = str
End With

End Sub

Frosty
08-12-2012, 09:55 AM
If I recall correctly (I'm not looking at a machine at the moment), I think .RowSource is meant for SQL queries.

Look into .AddItem or .List to add items to a list control .AddItem does them one by one, .List allows you to set a complete array to the list control in a single action.

gmaxey
08-12-2012, 12:25 PM
Private Sub UserForm_Initialize()
Dim asentence As Range
Dim str As String
Dim arrStr() As String
For Each asentence In ActiveDocument.Range.Sentences
If asentence.Words.Count > 1 Then
str = str & "|" & asentence.Words(1)
End If
Next
'Clip first pipe character
str = VBA.Right(str, Len(str) - 1)
arrStr() = Split(str, "|")
With UserForm1
.ListBox1.List = arrStr()
End With

End Sub

I'm now taking the next step and wanting to use a list of first words in long sentences to populate a column in a user form's listbox.

The following code seems as though it should work but it returns the error message "Could not set the RowSource property. Invalid property value." Not sure where I've gone wrong. The required value for a RowSource is a string, and I'm sending a string to it. Suggestions?


Sub FindStartOfLongSentenesAndPutInUserForm()
'This should list the first word of each sentence longer than 10 words in a user form Listbox

Dim asentence As Range
Dim str As String
For Each asentence In ActiveDocument.Range.Sentences
If asentence.Words.Count > 10 Then
str = str & asentence.Words(1)
End If
Next

With UserForm1
.ListBox1.RowSource = str
End With

End Sub

Jake Johnson
08-12-2012, 09:06 PM
Thanks so much. I keep wanting to make With statements to do too much.