Solved

Word 2010 VBA - Move the cursor one character to the right every 2 seconds

Posted on 2014-01-18
6
3,916 Views
Last Modified: 2014-01-22
I would like to have my cursor move to the right one character every two seconds until the end of document is reached.  At the end of the document, I would like a message box popping up showing the character count and the amount of time elapsed.
What is the Word VBA code to do this?
0
Comment
Question by:brothertruffle880
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39791772
Sub Demo()

    Dim start As Double, delay As Double
    Dim starttime As Double, endtime As Double
    Dim i As Integer


    starttime = Time

    For i = 1 To ActiveDocument.Characters.Count

        start = Timer
        delay = 2

        Selection.Move Unit:=wdCharacter, Count:=1

        Do Until Timer > start + delay
        Loop
    Next i

    endtime = Time

    MsgBox "This document has " & ActiveDocument.Characters.Count & " Characters." & vbCrLf & vbCrLf & "Time elapsed: " & Format(endtime - starttime, "HH:NN:SS")

End Sub

Open in new window

0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39791823
You beat me to it. Nice code - I was thinking of using application.ontime I like your approach  better
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39791996
Thank you.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:brothertruffle880
ID: 39792372
The timer works but the character count is giving me the count for the entire document.    I want a count of the number of characters my cursor is traversing across, Sometimes I'm starting my count in the middle of the document.  I need the count from the start point to the end of the document.
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39792399
Sub Demo()

    Dim start As Double, delay As Double
    Dim starttime As Double, endtime As Double
    Dim i As Integer


    starttime = Time

    For i = 1 To ActiveDocument.Characters.Count - Selection.Range.start

        start = Timer
        delay = 2

        Selection.Move Unit:=wdCharacter, Count:=1

        Do Until Timer > start + delay
        Loop
        
    Next i

    endtime = Time

    MsgBox "The cursor has moved " & i & " Characters." & vbCrLf & vbCrLf & "Time elapsed: " & Format(endtime - starttime, "HH:NN:SS")

End Sub

Open in new window

0
 

Author Closing Comment

by:brothertruffle880
ID: 39799421
Thank you.  Terrific.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now