Log in

View Full Version : Macro code for reading evalutaion project



arniel01
05-14-2013, 01:38 AM
Hello everyone! I have a school project that aims to evaluate reading time using Powerpoint presentation. I need to use macro to show the readers their reading time.

Can you please help me with this one. I am just new to Powerpoint macros.
I have searched the net but didn't got lucky.

Here's the code I tried to compose, I don't even think I got it right a bit.

Sub ReadingTime()
Dim Duration_Time As Date
Duration_Time = Now() - Start_time

//Here I must calculate the reading speed in words pr. minute (wpm).
//Words equal number of slides-3 (the first 2+the last).
//The formula is:
//Number of words / (divided by) Reading time (seconds) * 60 = WORDS PER MINUTE

//Finally, I must display a messages box with 2 lines saying: "You took: "+Reading time (Minutes and secs)
//and below "Your reading speed is" +Words Per Mimute
End Sub

John Wilson
05-14-2013, 06:57 AM
Probably best if you do your own schoolwork!

arniel01
05-14-2013, 07:02 PM
Yeah. Thanks for that reminder John.
I'm learning really a lot from here. "pptalchemy (dot) co (dot) uk/write_vba6. html"

I am not really sure if I can make it though.
Any help would be appreciated. :(

John Wilson
05-15-2013, 05:45 AM
If you at least post an attempt at the code maybe you will get some help.

arniel01
05-15-2013, 11:40 PM
Hello John,

This is what I got so far. I got a bug issue on the "iReading_Time = tWords / tTime / 60" line saying "Run time error'11' division by zero". :bug:
Can you help me with this? I need to show the reading evaluation words per minute.


Public Start_time As Date
Public End_Time As Date
____________________________________________________________
Sub Start()
'at action button click in the first slide the time starts counting
Start_time = Now()

End Sub
____________________________________________________________
Sub ReadingTime()
'at action button click on the last slide the evaluaton message appears
Dim Reading_Time As String
Dim iReading_Time As Integer
Dim tWords As Integer
Dim tTime As Integer

tTime = End_Time - Start_time
tWords = ActivePresentation.Slides.Count - 3
iReading_Time = tWords / tTime / 60
Reading_Time = iReading_Time

MsgBox "Evaluation : Your reading speed is " & Reading_Time & "words per minute"

End Sub

arniel01
05-15-2013, 11:43 PM
Guys, any help and tips would be much appreciated. I went through several tutorials in pptalchemy and office.com.

John Wilson
05-16-2013, 12:38 AM
OK

To get the elapsed time I would use the DateDiff function

Time_sec = DateDiff("n", Start_time, End_Time) '"n" sets the difference in MINUTES

Note that a value less than one minute will show as zero

Your calculation looks at the number of slide not the number of words

you can count the words in slides 4 to end like this

Public end_Time As Date
Public end_Time As Date
Public iWordCount As Integer

Sub count_words()
Dim i As Integer
Dim s As Integer
For i = 4 To ActivePresentation.Slides.Count
For s = 1 To ActivePresentation.Slides(i).Shapes.Count
' make sure shape can have words or it will error
If ActivePresentation.Slides(i).Shapes(3).HasTextFrame Then
iWordCount = iWordCount + ActivePresentation.Slides(i).Shapes(3).TextFrame.TextRange.Words.Count
End If
Next s
Next i
End Sub

Good luck

arniel01
05-20-2013, 06:44 AM
Thank you so much for the reply John. I edited the codes using your suggestion, I didn't include the count_words sub yet as my presentation set-up is something different.

My presentation is like this: There are around 500 slides in the presentation, the first slide is an introduction, second slide is for instructions with the action button for the Start_time macro - once clicked the time starts. Slides 3 - 499 has one word in each, so the reader will have to go through each of the slide in a period of time, that's why the word_count equals slide.count minus three. The last slide will have the action button that is when clicked will show the reading evaluation.

This is what I came up with.

Public Start_time As Date
Public End_time As Date
_________________________________________________________________
Sub Start_time()
'at action button click in the first slide the time starts counting
Start_time = Now()

End Sub
_________________________________________________________________
Sub ReadingTime()
'at action button click on the last slide the evaluaton message appears
Dim Reading_Time As String
Dim iTotal_time As Long
Dim Word_count As Integer


End_Time = Now()
iTotal_time = DateDiff("n", End_time, Start_time)
Word_count = ActivePresentation.Slides.Count - 3
Reading_Time = Word_count / iTotal_time


MsgBox "Evaluation : Your reading speed is " & Reading_Time & "words per minute"

End Sub

This code gives a result but not accurate. I mean, I tried it in the presentation with different pace(scanning through the slides slowly and fast) and it gives the same result.

I think the problem is getting the Start_time value from the sub.
Also, I was suggested by a friend to check the value easier by using a MsgBox. I am not sure if I get it right but I was told that it would look like this : MsgBox Start_time call. She just told me to research about it.

John Wilson
05-20-2013, 07:21 AM
Probably because the TimeDiff as I coded measures in whole minutes and will be a whole minute out

To make it measure in seconds change "n" to "s" and when you have the total second do a little math to get the minutes.

You also need to alter the Date Diff line ORDER
iTotal_time = DateDiff("s", Start_time,End_Time)

Note Start is first

Also when I copy your code I see some added divider lines? I don't know if you added them or vbax but they will stop the code running correctly.

and last ALSO

DO NOT use the same name for the variable Start_Time and the Sub Start_Time. This will definitely confuse things call one something else say Sub Run_Timer()

arniel01
05-20-2013, 08:25 AM
Hello John, thank you for the suggestion. I edited the code according to your suggestion.

Public Start_time As Date
Public End_time As Date
Public iWord_count As Integer
Sub Run_timer()
'at action button click on the second slide the time starts
Start_time = Now()
End Sub
Sub ReadingTime()
'at action button click on the last slide the evaluaton message appears
Dim Reading_Time As String
Dim iTotal_time As Long

End_time = Now()
iTotal_time = DateDiff("s", Start_time, End_time)
iWord_count = ActivePresentation.Slides.Count - 3
Reading_Time = iWord_count / iTotal_time * 60


MsgBox "Evaluation : Your reading speed is " & Reading_Time & "words per minute"

End Sub

Error '6' appears and turns yellow the iTotal_time = DateDiff("s", Start_time, End_time) line.

John Wilson
05-20-2013, 10:42 AM
If you are getting a 6 error (overflow) chances are for some reason Start_Time has not been set and is still Null

add a line to check
Sub Run_timer()
'at action button click on the second slide the time starts
Start_time = Now()
MsgBox Start_Time
End Sub

Have a look at my demo I posted in the previous post to see how I "locked" on click transition so the button had to be clicked.

Look for those lines I mentioned as they may stop the code running. They are under the lines that PPT adds between subs so hard to see. Turn off the proper lines in the vb editor > Tools > Options > Show procedure separator

arniel01
05-21-2013, 07:03 PM
Hello John,

Thank you so much for helping me with my code.
Your suggestions were really helpful. :)