Doevents in Word 2003

J

Joe Fawcett

Has anyone had problems with this? On two machines I have, both with latest
releases and sp1 for Ofiice 2003 the following code runs 'like a dog' and
hogs 100% of cpu cycles, even with no other user processes executing. The
letters come out very, very slowly. Usually the first line comes after a
long wait, the next part comes regularly as is hoped for.

Private Sub TypeString(str As String, delay As Long)
Dim iIndex As Long
Dim iStringLength As Long
Dim oDoc As Document
Set oDoc = ActiveDocument
Dim oRange As Range
Set oRange = oDoc.Content
oRange.Text = ""
iStringLength = Len(str)
For iIndex = 1 To iStringLength
oRange.Collapse wdCollapseEnd
oRange.Text = Mid$(str, iIndex, 1)
DoPause delay
Next iIndex
End Sub

Private Sub DoPause(secs As Long)
Dim fFinish As Single
fFinish = Timer + secs
Do While Timer < fFinish
DoEvents
Loop
End Sub

Public Sub ShowMessage()
TypeString "Happy New Year" & vbCrLf & "from" & vbCrLf & "Joe", 2
End Sub

I run ShowMessage via alt+F8 and double clicking list box entry.
Perhaps there is something else wrong with the code?

Thanks
 
M

Malcolm Smith

Joe

I wouldn't say that it was hogging the CPU. The loop is going around and
around all the time. That's fair enough; but the DoEvents is making sure
that nothing else is blocked.

So, the CPU is going to be busy for 2 seconds per character doing nothing
but to go in a loop and cast a DoEvents.

If you ran something else then that second application wouldn't take any
longer because the Word application isn't hogging the CPU.

Do you really mean to spend a two second pause between each character?

Also, note that you have in your DoPause() function a variable which holds
a Single type but you put into it a Long type. Do you mean to do this?

- Malc
 
J

Joe Fawcett

Malcolm Smith said:
Joe

I wouldn't say that it was hogging the CPU. The loop is going around and
around all the time. That's fair enough; but the DoEvents is making sure
that nothing else is blocked.

So, the CPU is going to be busy for 2 seconds per character doing nothing
but to go in a loop and cast a DoEvents.

If you ran something else then that second application wouldn't take any
longer because the Word application isn't hogging the CPU.

Do you really mean to spend a two second pause between each character?

Also, note that you have in your DoPause() function a variable which holds
a Single type but you put into it a Long type. Do you mean to do this?

Not sure what you mean. Timer returns a single, I add on a long for the
finish time.
What I mean is that the letters don't come out at a regular pace, I was
trying to get a two second pause between each letter, what I get is periods
of inactivity followed by five or six leters at a time. When I look in task
manager my machine is running at 100%.

Thanks for the help.
 
M

Malcolm Smith

The 100% CPU time is because of your Timer loop. It's spending 100% of
the time doing lots of nothing and you've made it busy doing nothing.
But it's not hogging the CPU; it's only running the Word code when there
is nothing else to do.

I think that the problem which you are having is because of Word
repainting; if you added code which would do the repaint yourself then it
may help.

Look back in these forums for a routine which I called PreventWordDeath()
which I wrote which may help. A quick Google ought to locate it.

Sorry, can't do it now myself, am on the way to bed.

- Malc
 
J

Joe Fawcett

Malcolm Smith said:
The 100% CPU time is because of your Timer loop. It's spending 100% of
the time doing lots of nothing and you've made it busy doing nothing.
But it's not hogging the CPU; it's only running the Word code when there
is nothing else to do.

I think that the problem which you are having is because of Word
repainting; if you added code which would do the repaint yourself then it
may help.

Look back in these forums for a routine which I called PreventWordDeath()
which I wrote which may help. A quick Google ought to locate it.

Sorry, can't do it now myself, am on the way to bed.

- Malc

Thank you so much, manupulating the screenUpdating manually did the trick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top