Hi Malc,
If nothing else, I am a good student!
I investigated the Collection object, then I thought I put it into practice.
I imagined the following scenario:
In a multi-select list box on a UserForm, I want to list all bold words from
a "simple" document , select the words I want to Unbold and click on a
button to do so...
Now remember that my purpose here is to practice fooling around with a
collection object.
So I am not claiming that the way I do it below is the best way...
So here is my code and a question follows:
In a standard module:
'_______________________________________
Public Const ItemSeparatorLen As Integer = 11
'The following constant is to separate the word from its index
'position in the document so I can retrieve it later.
Public Const ItemSeparator As String = "_myNumber_#"
Public BoldWords As New Collection
'_______________________________________
Sub callForm()
Dim i As Integer
With ActiveDocument.Range
For i = 1 To .Words.Count
'And ... > 1 to eliminate paragraph marks and other single character that
Word
'considers as words... I realize that in this simple code I am also
'eliminating words like "a", "I" and so on... but for now it does the job...
If .Words(i).Bold And Len(.Words(i).Text) _
BoldWords.Add .Words(i).Text & _
ItemSeparator & i
End If
Next i
End With
'never mind magic forms for now... it is just a test on a
'single simple document!
'A user form with a list box and 2 buttons
Load UserForm1
UserForm1.Show
End Sub
'_______________________________________
In the UserForm1 code page:
'_______________________________________
Private Sub Cancelcmd_Click()
Unload Me
End Sub
'_______________________________________
Private Sub UnBoldcmd_Click()
Dim i As Long
Dim ItemtoUnBold As Variant
Dim MyNumberPos As Long
For i = 0 To BoldWords.Count - 1
If ListBox1.Selected(i) Then
ItemtoUnBold = BoldWords.Item(i + 1)
MyNumberPos = InStr(1, ItemtoUnBold, _
ItemSeparator)
ItemtoUnBold = Mid(ItemtoUnBold, _
MyNumberPos + ItemSeparatorLen)
ActiveDocument.Range.Words(ItemtoUnBold) _
.Bold = False
End If
Next i
Unload Me
End Sub
'_______________________________________
Private Sub UserForm_Activate()
Dim i As Integer
Dim AddedItem As String
Dim MyNumberPos As Long
For i = 1 To BoldWords.Count
AddedItem = BoldWords.Item(i)
MyNumberPos = InStr(1, AddedItem, _
ItemSeparator)
AddedItem = Left(AddedItem, _
MyNumberPos - 1)
ListBox1.AddItem AddedItem
Next i
End Sub
'_______________________________________
The collection objects behaves as expected and is indeed useful... Thanks
for bringing it to my attention. OTH, I will wait before I venture coding
with collections of collections to simulate multidimensional arrays! But I
see how it could be done.
My question is (Nothing to do with collections!):
in the code that gathers the items to build the collection:
For i = 1 To .Words.Count
If .Words(i).Bold And Len(.Words(i).Text) _
BoldWords.Add .Words(i).Text & _
ItemSeparator & i
End If
Next i
The "For i = 1 To .Words.Count" loop is very slow...
My simple test document had only 448 words on two pages... and it was
already atrociously slow...
Any fast way to iterate through the words collection? A different kind of
loop?
Remember that I need to store the index position of each word I pick up so I
can refer to it later to unbold it if I choose to do so...
TIA
Have a good weekend in frosty Wales!