MS Word&VBA >> StrReverse() function - infinite loop

by mike510 » Sun, 03 Oct 2004 01:34:31 GMT

I have just started program with Word 2002
While trying to reverse each word in a line of text encountered a
problem: Word enters an infinite loop and stops responding.
Could somebody explain why?
Code:
---------------
Sub TestReverseWords()
Dim myRange As Range
Dim aWord As Object
Set myRange = ActiveDocument.Range(Start:=0, End:=Selection.End)
For Each aWord In myRange.Words
aWord.Text = StrReverse(aWord.Text)
Next aWord
End Sub
--------------
end code

Thanks in advance.
Mike


MS Word&VBA >> StrReverse() function - infinite loop

by Jay Freedman » Sun, 03 Oct 2004 04:27:20 GMT

Hi Mike,

Admittedly this is not intuitive at all. What Word considers a 'word'
isn't the usual definition. Each member of the Words collection
includes all the spaces that follow the word. It may also be a series
of punctuation characters. Most important in this discussion, it may
also be a sequence of space characters at the beginning of a document,
up to but not including the first nonspace character.

In the VBA editor, right-click aWord and choose Add to Watch. Now use
F8 to single-step your macro. After the first StrReverse call, the
space that used to follow the first word is now at the beginning of
the document. On the next time through the loop, aWord consists of the
range of just that single space character (you can check its Start and
End to see that they're 0 and 1, respectively). Then StrReverse just
'reverses' that space again and again in an infinite loop. (Tip: to
break out of the loop, press Ctrl+Break.)

An unintuitive thing about this is that the For Each recomputes the
location of the 'next word' on each iteration, starting from the Start
of the document. This is not like a standard For loop, which fixes the
test condition during the first iteration and never again.

A workaround is to use a For loop that counts backward through the
Words collection. In addition, you need some code to move the space
character from its new position at the beginning of the word to its
old position, to prevent all the nonspace characters from being jammed
together. Try this version:

Sub TestReverseWords2()
Dim myRange As Range
Dim aWord As Range
Dim nCount As Long
Set myRange = ActiveDocument.Range(Start:=0, End:=Selection.End)
For nCount = myRange.Words.Count To 1 Step -1
Set aWord = myRange.Words(nCount)
aWord.Text = StrReverse(aWord.Text)
If (aWord.Characters.First = " ") Then
aWord.Text = Mid$(aWord.Text, 2) & " "
End If
Next nCount
End Sub






Similar Threads

1. API VBA: Infinite Loop

2. Problem with Form.ValidateChildren (infinite validation loop?)

 When attempting to use the form.ValidateChildren() with multiple validation 
errors the code appears to get in an infinite loop among the various 
validation subroutines.  With only one validation error on the form the 
problem does not appear to occur.  Thank you for your help on this issue.

-- 
Jeffrey A Heimberger, MCP
Information Systems Assistant 
Stark Area Regional Transit Authority (SARTA) 
Ph. 330-454-6132 x 526 -- fax 330-454-5476 
Voted #1 Transit in America 

3. Infinite loop querying remote WMI

4. Infinite loop with crystal control viewer

Hi:
Here in my company we have been trying to figure out (for several days
already) what is the weird reason that is doing the crystal control
viewer (crviewer.dll) act this way.

When we are previewing a report (that is using an Access Database as it
source), we call this snippet of code...

    With CRViewer1
        .EnableDrilldown = False
        .EnableGroupTree = False
        .Top = 0
        .Left = 0
        .Height = ScaleHeight
        .Width = ScaleWidth
        .Zoom (100)
        .DisplayBackgroundEdge = True
        .DisplayTabs = False
        .ReportSource = m_crReport
        .ViewReport 'Here, the VB.EXE Process jumps to 100% usage
    End With

Just after we call CRViewer1.ViewReport method, the application hangs
and VB6.EXE jumps in the CPU Usage (verified through Task Manager).

There is no way to close the app (or Stop VB6), the only way to start
again is kill the process and start again... It's getting us crazy!

Here is some information about our environment
- Running on W2K
- Applied latest set of MDAC and OLEDB JET libraries (just in case it
was an Access problem) - Jet 4.0 Service Pack 8 -
- This error happens regardless whether we are in the VB IDE or if the
app is compiled.

Please, if anyone had experienced a similar problem, I would really  be
thankful for your help.

Any idea would be appreciated.

5. Calling code in DLL that has an "infinite Loop" - Visual Basic/VB

6. Infinite While Loops

Try this...

     While ucase(right(select.text, 10) <> "EVENT DATE"
             Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
     Wend

Mark



7. Weird infinite loop - Visual Basic/VB

8. XP Setup in an infinite loop

I tried to repair XP by upgrading it.  Hoping to install a good copy over a 
suspect one and not have it destroy all my installed programs and files.  At 
the 39th minute of the setup procedure I received a Fatal Error warning.  The 
setup stop, shows me the setup error log, then it closes out and reboots the 
computer.  The computer starts at the exact same place and repeats the 
process.  It is like an infinite loop.  I have pushed every F-key and I have 
shut off the computer and turned it back on to no avail.  It startsd the 
process over.  How can I stop this?  I would rather run a buggy version of XP 
than to be in this infinite-loop Hell.