Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Can macro highlight & pause on each word in document?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
FNader - 06 Oct 2007 05:32 GMT
Want to assist ADHD grandson focus on reading tasks. Would like to create
macro that "reads" word document one word at a time, pausing on each word.
Would like to see font size increase and/or highlighting to emphasize that
word within the sentence.  Ideally, macro would then pause until triggered to
proceed either manually (eg.; touch spacebar) and/or timeout (adjustable).
The latter feature intended to move him along after having become acquainted
with sentence content.
Jean-Guy Marcil - 06 Oct 2007 06:22 GMT
FNader was telling us:
FNader nous racontait que :

> Want to assist ADHD grandson focus on reading tasks. Would like to
> create macro that "reads" word document one word at a time, pausing
[quoted text clipped - 4 lines]
> intended to move him along after having become acquainted with
> sentence content.

Try this:

'_______________________________________
Option Explicit
'_______________________________________
Sub ReadWords()

Dim wdsDoc As Words
Dim i As Long
Dim strPause As String

Do
   strPause = InputBox("How long would you like to wait for each word?",
"Set Timer")
Loop While Not IsNumeric(strPause)

Set wdsDoc = ActiveDocument.Words

For i = 1 To wdsDoc.Count
   With wdsDoc(i)
       If .Characters(1) Like "[!A-z]" Then GoTo SkipWord
       .HighlightColorIndex = wdYellow
       .Bold = True
       .Font.Size = .Font.Size + 6

       WaitABit CSng(strPause)
       DoEvents
       ActiveDocument.UndoClear
       Application.ScreenRefresh

       .HighlightColorIndex = wdNoHighlight
       .Bold = False
       .Font.Size = .Font.Size - 6
   End With
SkipWord:
Next

End Sub
'_______________________________________

'_______________________________________
Sub WaitABit(sngWaitSecs As Single)

Dim myDate As Date

myDate = Timer
Do
Loop Until (Timer - myDate) > sngWaitSecs

End Sub
'_______________________________________

Use ALT-F8 to lauch the macro "ReadWords" and CTFL=BREAK followed by End to
stop it.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg Maxey - 06 Oct 2007 10:54 GMT
> FNader was telling us:
> FNader nous racontait que :
[quoted text clipped - 70 lines]
> jmarcilREM...@CAPSsympatico.caTHISTOO
> Word MVP site:http://www.word.mvps.org

JGM,

Haven't figured out why, but if I run this code it doesn't highlight
the last word in the document.  If I step through, it does????
Greg Maxey - 06 Oct 2007 11:09 GMT
JGM,

Added another Application.ScreenRefresh an problem is resolved.

Also it seems I was advised once to avoid GoTo statements.  In view of that
advice and since the code evaluates .Characters(1) anyway do you think

If Not .Characters(1) Like "[!A-z]" Then

would be just as good?

Sub ReadWords()
Dim wdsDoc As Words
Dim i As Long
Dim strPause As String
Do
 strPause = InputBox("How long would you like to wait for each word?", "Set
Timer")
Loop While Not IsNumeric(strPause)
Set wdsDoc = ActiveDocument.Words
For i = 1 To wdsDoc.Count
 With wdsDoc(i)
   If Not .Characters(1) Like "[!A-z]" Then
     .HighlightColorIndex = wdYellow
     .Bold = True
     .Font.Size = .Font.Size + 6
     Application.ScreenRefresh 'Added this line.
     WaitABit CSng(strPause)
     DoEvents
     ActiveDocument.UndoClear
     Application.ScreenRefresh
     .HighlightColorIndex = wdNoHighlight
     .Bold = False
     .Font.Size = .Font.Size - 6
   End If
 End With
Next
End Sub
Sub WaitABit(sngWaitSecs As Single)
Dim myDate As Date
myDate = Timer
Do
Loop Until (Timer - myDate) > sngWaitSecs
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> FNader was telling us:
>> FNader nous racontait que :
[quoted text clipped - 75 lines]
> Haven't figured out why, but if I run this code it doesn't highlight
> the last word in the document.  If I step through, it does????
Jean-Guy Marcil - 06 Oct 2007 20:30 GMT
Greg Maxey was telling us:
Greg Maxey nous racontait que :

> JGM,
>
[quoted text clipped - 6 lines]
>
> would be just as good?

Yes, of course, it is just that it was late last night, I started coding one
way, did not like it, changed it and those last changes made the GoTo
unnecessary, but it was too late in the night for me to even notice that!

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg Maxey - 06 Oct 2007 11:22 GMT
JGM,

Also need to change [!A-z] to [!AZaz] to prevent highlighting the unicode
characters 90 through 95  "[ \ ] ^ _ `"

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> FNader was telling us:
>> FNader nous racontait que :
[quoted text clipped - 75 lines]
> Haven't figured out why, but if I run this code it doesn't highlight
> the last word in the document.  If I step through, it does????
Helmut Weber - 06 Oct 2007 13:27 GMT
Hi Greg,

>Also need to change [!A-z] to [!AZaz] to prevent..

you mean, to change to

Like "[!A-Za-z]"

besides that, the code seems to work perfectly
for plain english.
Lucky you, you haven't to deal with localization issues.

Cheers

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Greg Maxey - 06 Oct 2007 13:33 GMT
Helmut,

Yep.  As usual typos abound on my end.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi Greg,
>
[quoted text clipped - 9 lines]
>
> Cheers
FNader - 06 Oct 2007 19:10 GMT
Very nice to get your input!
Am new to discussion group and not yet entirely clear on protocals.
I posted reply to Jean-Guy Marcil (1st responder) with additional questions.
If you have time your input would be valued.
Thanks again
Frank

> Hi Greg,
>
[quoted text clipped - 9 lines]
>
> Cheers
FNader - 06 Oct 2007 19:11 GMT
Very nice to get your input!
Am new to discussion group and not yet entirely clear on protocals.
I posted reply to Jean-Guy Marcil (1st responder) with additional questions.
If you have time your input would be valued.
Thanks again
Frank

> JGM,
>
[quoted text clipped - 80 lines]
> > Haven't figured out why, but if I run this code it doesn't highlight
> > the last word in the document.  If I step through, it does????
Jean-Guy Marcil - 06 Oct 2007 20:31 GMT
Greg Maxey was telling us:
Greg Maxey nous racontait que :

> JGM,
>
> Also need to change [!A-z] to [!AZaz] to prevent highlighting the
> unicode characters 90 through 95  "[ \ ] ^ _ `"

Good point, I forgot that there were a few characters between the upper and
lower case characters.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

FNader - 06 Oct 2007 18:32 GMT
Thanks much!!
Very helpful ... but gave rise to a few questions...
1) As codes processes, it runs past current screen view.
Is the a way to keep processing centered (vertically) in current screen area
without having grandson distracted by need to advance with vertical scrollbar?
2) It seems that he needs more time with some words and is pressured on
first read when exclusively using time out advance feature. What would be
needed to include manual advance option to allow whatever time is needed on
each word when first reading document?
3) Can the script be initialte from point of curser insertion so that we
don't always have to go back to the top of the document when resuming read?
Thanks also to other contributors .. will reply separately
Frank

> FNader was telling us:
> FNader nous racontait que :
[quoted text clipped - 62 lines]
> Use ALT-F8 to lauch the macro "ReadWords" and CTFL=BREAK followed by End to
> stop it.
Jean-Guy Marcil - 06 Oct 2007 20:34 GMT
FNader was telling us:
FNader nous racontait que :

> Thanks much!!
> Very helpful ... but gave rise to a few questions...
> 1) As codes processes, it runs past current screen view.
> Is the a way to keep processing centered (vertically) in current
> screen area without having grandson distracted by need to advance
> with vertical scrollbar?

Yes, it would be possible, but it would require more code than I have time
to do right now.
I'll look at tit later next week, unless somebody has  a suggestion before
then!

2) It seems that he needs more time with
> some words and is pressured on
> first read when exclusively using time out advance feature. What
> would be needed to include manual advance option to allow whatever
> time is needed on each word when first reading document?

The code would have to be totally different.
I guess a dialog box would be needed, or some fancy toolbar button work...

> 3) Can the script be initialte from point of curser insertion so that
> we don't always have to go back to the top of the document when
> resuming read? Thanks also to other contributors .. will reply
> separately

See reply to point 1).

Sorry, but I do not have time now... I'll mark this thread and come back
early next week.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg Maxey - 07 Oct 2007 03:51 GMT
I think this takes care of 1 and 3.  Haven't had time to think about 2 yet.
Sub ReadWords()
Dim oRng As Word.Range
Dim wdsDoc As Words
Dim i As Long
Dim strPause As String
Do
 strPause = InputBox("How long would you like to wait for each word?", "Set
Timer ")
Loop While Not IsNumeric(strPause)
Set oRng = ActiveDocument.Range
oRng.Start = Selection.Range.Start
Set wdsDoc = oRng.Words
For i = 1 To wdsDoc.Count
 With wdsDoc(i)
   If Not .Characters(1) Like "[!A-Za-z]" Then
      wdsDoc.Item(i).Select
      ActiveWindow.ScrollIntoView Selection.Range, True
      Selection.Collapse wdCollapseEnd
      .HighlightColorIndex = wdYellow
     .Bold = True
     .Font.Size = .Font.Size + 6
     Application.ScreenRefresh
     WaitABit CSng(strPause)
     DoEvents
     ActiveDocument.UndoClear
     Application.ScreenRefresh
     .HighlightColorIndex = wdNoHighlight
     .Bold = False
     .Font.Size = .Font.Size - 6
   End If
 End With
Next
End Sub
Sub WaitABit(sngWaitSecs As Single)
Dim myDate As Date
myDate = Timer
Do
Loop Until (Timer - myDate) > sngWaitSecs
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Thanks much!!
> Very helpful ... but gave rise to a few questions...
[quoted text clipped - 86 lines]
>> jmarcilREMOVE@CAPSsympatico.caTHISTOO
>> Word MVP site: http://www.word.mvps.org
Russ - 07 Oct 2007 05:28 GMT
Greg,
One way to do 2 might be to create a userform the size of a single resume
button that gets placed in the upper left corner of the screen when strPause
= 0 and tested for in the WaitABit sub. On my MacWord 2004, at home now, I
can't export the .bas , .frm, and frx files to post such a userform code.
But if it shown modal then it should pause the loop code.

> I think this takes care of 1 and 3.  Haven't had time to think about 2 yet.
> Sub ReadWords()
[quoted text clipped - 36 lines]
> Loop Until (Timer - myDate) > sngWaitSecs
> End Sub

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 07 Oct 2007 05:35 GMT
I forgot to mention that by using that method, you advance to the next word
by pressing the enter key. The button would be the default button and its
code would just be: Unload Me

> Greg,
> One way to do 2 might be to create a userform the size of a single resume
[quoted text clipped - 43 lines]
>> Loop Until (Timer - myDate) > sngWaitSecs
>> End Sub

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Greg Maxey - 07 Oct 2007 06:15 GMT
Russ,

I was too far into my own crude solution to back up and take a hard look at
yours.  Perhaps if this was a process that "I" really needed I might give if
the extra effort.

I have scratched together some crude code that appears to do what I
understood the OP requested:

'Run from Menu, hotkey or toobar
Sub RunMan()
ReadWords False
End Sub

'Run from Menu, hotkey or toobar
Sub RunAuto()
ReadWords True
End Sub

Sub ReadWords(ByRef bTest As Boolean)
Dim oRng As Word.Range
Dim wdsDoc As Words
Dim i As Long
Dim strPause As String
Dim bAutoTimed As Boolean
Dim pRng As Word.Range

Set oRng = ActiveDocument.Range
oRng.Start = Selection.Range.Start
bAutoTimed = bTest
Set wdsDoc = oRng.Words
If bAutoTimed Then
 Do
   strPause = InputBox("How long would you like to wait for each word?",
"Set Timer ")
 Loop While Not IsNumeric(strPause)
 For i = 1 To wdsDoc.Count
   With wdsDoc(i)
     If Not .Characters(1) Like "[!A-Za-z]" Then
       wdsDoc.Item(i).Select
       ActiveWindow.ScrollIntoView Selection.Range, True
       Selection.Collapse wdCollapseEnd
       .HighlightColorIndex = wdYellow
       .Bold = True
       .Font.Size = .Font.Size + 6
       Application.ScreenRefresh
       WaitABit CSng(strPause)
       DoEvents
       ActiveDocument.UndoClear
       Application.ScreenRefresh
       .HighlightColorIndex = wdNoHighlight
       .Bold = False
       .Font.Size = .Font.Size - 6
     End If
   End With
 Next
Else
 For i = 1 To wdsDoc.Count
   With wdsDoc(i)
     If Not .Characters(1) Like "[!A-Za-z]" Then
     .Select
     Exit For
     End If
   End With
 Next
 On Error GoTo Err_Handler
 Set pRng = wdsDoc(i)
 pRng.Start = ActiveDocument.Range.Start
 With pRng.Find
   .Font.Bold = True
   .Highlight = True
   .Font.Size = Selection.Font.Size + 6
   While .Execute
     With pRng
       If .HighlightColorIndex = wdYellow Then
         .Font.Bold = False
         .HighlightColorIndex = wdNoHighlight
         .Font.Size = .Font.Size - 6
       End If
     End With
   Wend
 End With
 ActiveWindow.ScrollIntoView Selection.Range, True
 With wdsDoc(i)
   .HighlightColorIndex = wdYellow
   .Font.Bold = True
   .Font.Size = Selection.Font.Size + 6
   .Collapse wdCollapseEnd
 End With
 Selection.Collapse wdCollapseEnd
 Application.ScreenRefresh
End If
Exit Sub
Err_Handler:
'When there are no more valid words an error will occur. _
'Now need to figure out how to clean up last highlighted word.
End Sub

Sub WaitABit(sngWaitSecs As Single)
Dim myDate As Date
myDate = Timer
Do
Loop Until (Timer - myDate) > sngWaitSecs
End Sub

One nagging issue remains.  When stepping through manually went there are no
more valid words the code will throw and error (a good thing).  I need to
figure out how to clear the special formatting on the last word processed.

I am done for now but would be interested in what others might do with this.
Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Greg,
> One way to do 2 might be to create a userform the size of a single
[quoted text clipped - 44 lines]
>> Loop Until (Timer - myDate) > sngWaitSecs
>> End Sub
Helmut Weber - 07 Oct 2007 13:32 GMT
Dear friends,

sorry for not looking into details
of the code you provided.

I was too much obsessed with my own way.

I wanted to go word by word through a text,
stop when there was a word to be looked at more closely,
and continue afterwards.

I used the escape key to stop,
and the escape key again to continue.

Still, you got to hit the right moment
to stop processing the text further.
But I had no problems after a few tries.

I have omitted font change to bold or to another
font size, as this might result in repaginating
or new lines etc ...

Defining the text to be processed from the
insertion point til the end of doc,
I regard as a minor step.

I'm exhausted.

' ---------------------------------------
Option Explicit
Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
' ---------------------------------------
Sub HighlightWordsAndWait()
Dim BlnCont As Boolean
Dim EscKey As Long
Dim rWrd As Range
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
For Each rWrd In rDcm.Words
With rWrd
 If .Characters(1) Like "[A-Za-z]" Then
   .HighlightColorIndex = wdYellow
   ActiveWindow.ScrollIntoView rWrd
   WaitABit CSng("1")
   EscKey = GetAsyncKeyState(&H1B)
   If EscKey <> 0 Then
      BlnCont = False
   Else
      BlnCont = True
   End If
   While BlnCont = False
      EscKey = GetAsyncKeyState(&H1B)
      If EscKey <> 0 Then
         BlnCont = True
      End If
   Wend
   WaitABit CSng("1")
   .Bold = False
   .HighlightColorIndex = wdNoHighlight
 End If
End With
Next
End Sub
' -------------------------------------
Sub WaitABit(sngWaitSecs As Single)
Dim Secnds As Long
Secnds = Timer
While Timer < Secnds + sngWaitSecs
Wend
End Sub
' -------------------------

Have a nice day everybody.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.