Hello.
I need to find all words (only upper case) from text and do some list
of found words. I'm used my macro, bot it had cycled. I can't found my
wrong. Please give me any tips. Thank you. That my code:
Sub UcaseList()
Dim wu As Word.Range
Dim myArr() As String
Dim i As Long

With Selection
.EndKey wdStory
.TypeParagraph
End With

For Each wu In ActiveDocument.Words
If wu.Case = wdUpperCase Then
myArr = Split(wu)
Selection.InsertAfter myArr(i) & vbCr
End If
Next wu
End Sub

RE: Why is cycling in my code? by JeanGuyMarcil

JeanGuyMarcil
Fri Jul 18 05:24:02 PDT 2008

"avkokin" wrote:

> Hello.
> I need to find all words (only upper case) from text and do some list
> of found words. I'm used my macro, bot it had cycled. I can't found my
> wrong. Please give me any tips. Thank you. That my code:
> Sub UcaseList()
> Dim wu As Word.Range
> Dim myArr() As String
> Dim i As Long
>
> With Selection
> .EndKey wdStory
> .TypeParagraph
> End With
>
> For Each wu In ActiveDocument.Words
> If wu.Case = wdUpperCase Then
> myArr = Split(wu)
> Selection.InsertAfter myArr(i) & vbCr
> End If
> Next wu
> End Sub

A few problems with your code:
1) Split creates a one-dimensional array by separating sub-strings using a
delimiter. If no delimiter is used in the function (as in your case) the
space character is assumed to be the delimiter. In your code, "wu" always
returns a single word, usually followed by a space, so "myArr = Split(wu)"
always means that myArr only contains exactly the same a "wu", minus the
space if there was one...
2) Becasue of the 1), "i" is always 0
3) These two considerations are minor. This next one is not! "For Each wu In
ActiveDocument.Words" cycles through all woprds in the current document.
Then, "Selection.InsertAfter myArr(i) & vbCr" keeps inserting words at the
end of the document... You see the problem... Your code keeps finding new
words because your code keeps adding words to the document...

Try this: (No array is used becasue I have no idea why you want to use an
array...)


Sub UcaseList()

Dim rngDoc As Word.Range
Dim wu As Word.Range
Dim lngDocEnd As Long

Set rngDoc = ActiveDocument.Range
lngDocEnd = rngDoc.End

For Each wu In rngDoc.Words
If wu.Case = wdUpperCase Then
ActiveDocument.Range.InsertAfter vbCrLf & wu.Text
rngDoc.End = lngDocEnd
End If
Next wu

End Sub


RE: Why is cycling in my code? by stevencraigmiller(at)comcast(dot)net>

stevencraigmiller(at)comcast(dot)net>
Fri Jul 18 05:27:00 PDT 2008

To: Avkokin,

Sub CreateListOfUppercaseWords()
Dim oRange As Range
Dim wd As Word.Range
Dim i As Long
Dim sList() As String

ReDim sList(0) As String
For Each wd In ActiveDocument.Words
If wd.Case = wdUpperCase Then
i = UBound(sList) + 1
ReDim Preserve sList(0 To i) As String
sList(i) = wd.Text
End If
Next wd
If UBound(sList) > 0 Then
Set oRange = ActiveDocument.Range
oRange.Collapse wdCollapseEnd
oRange.InsertAfter vbCr
For i = 1 To UBound(sList)
oRange.InsertAfter sList(i) & vbCr
Next i
End If
End Sub

Steven Craig Miller

"avkokin" wrote:

> Hello.
> I need to find all words (only upper case) from text and do some list
> of found words. I'm used my macro, bot it had cycled. I can't found my
> wrong. Please give me any tips. Thank you. That my code:
> Sub UcaseList()
> Dim wu As Word.Range
> Dim myArr() As String
> Dim i As Long
>
> With Selection
> .EndKey wdStory
> .TypeParagraph
> End With
>
> For Each wu In ActiveDocument.Words
> If wu.Case = wdUpperCase Then
> myArr = Split(wu)
> Selection.InsertAfter myArr(i) & vbCr
> End If
> Next wu
> End Sub
>

Re: Why is cycling in my code? by Helmut

Helmut
Fri Jul 18 05:36:38 PDT 2008

Hi Anton,

it is because the inserted words
are again processed by your macro.

Have a look at that one:

Sub UcaseListB()
Dim rWrd As Word.Range
Dim rTmp As Word.Range
Dim myArr() As String
Dim x As Long
ActiveDocument.Range.InsertAfter vbCr
Set rTmp = Selection.Range
rTmp.start = 0
x = ActiveDocument.Range.End - 1
For Each rWrd In rTmp.Words
If rWrd.Case = wdUpperCase Then
myArr = Split(rWrd)
ActiveDocument.Range.InsertAfter myArr(0) & vbCr
rTmp.start = 0
rTmp.End = x
End If
Next
End Sub

If that is your doc:
The quick brown FOX jumps over the lazy DOG.¶

That is the result:
The quick brown FOX jumps over the lazy DOG.¶
FOX¶
DOG¶


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP

Re: Why is cycling in my code? by avkokin

avkokin
Fri Jul 18 05:51:31 PDT 2008

On 18 $B'Z'p'](B, 16:36, Helmut Weber <red....@t-online.de> wrote:
> Hi Anton,
>
> it is because the inserted words
> are again processed by your macro.
>
> Have a look at that one:
>
> Sub UcaseListB()
> Dim rWrd As Word.Range
> Dim rTmp As Word.Range
> Dim myArr() As String
> Dim x As Long
> ActiveDocument.Range.InsertAfter vbCr
> Set rTmp = Selection.Range
> rTmp.start = 0
> x = ActiveDocument.Range.End - 1
> For Each rWrd In rTmp.Words
> If rWrd.Case = wdUpperCase Then
> myArr = Split(rWrd)
> ActiveDocument.Range.InsertAfter myArr(0) & vbCr
> rTmp.start = 0
> rTmp.End = x
> End If
> Next
> End Sub
>
> If that is your doc:
> The quick brown FOX jumps over the lazy DOG.$B"y(B
>
> That is the result:
> The quick brown FOX jumps over the lazy DOG.$B"y(B
> FOX$B"y(B
> DOG$B"y(B
> $B"y(B
>
> --
>
> Greetings from Bavaria, Germany
>
> Helmut Weber, MVP WordVBA
>
> Vista Small Business, Office XP

Thank you very much for your postings! I'm learn by one's own mistakes.