PDA

View Full Version : Solved: automatic scroll after "xx" of seconds



aloy78
10-02-2011, 07:15 PM
Dear all,

I would like to have a "macro button script" that does an auto scroll after "xx" number of seconds. The idea is this.
Once I click on the start button, it will start to scroll down slowly (prefer to have option to adjust the speed of the scroll) after say "5 seconds". I'm doing this for a solo guitar stage performance and i wouldn't want to be caught offguard while i have to stop and scroll. :rotlaugh: :*)

gmaxey
10-02-2011, 08:07 PM
Try:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub ScratchMacro()
Dim i As Long
Dim j As Long
Dim oPage As Page
For Each oPage In ActiveDocument.ActiveWindow.ActivePane.Pages
i = i + oPage.Rectangles(1).Lines.Count
Next oPage
Do
Doze 5000 '= 5 seconds
ActiveDocument.ActiveWindow.SmallScroll Down:=5 'five lines
j = j + 5
Loop Until j > i
End Sub
Sub Doze(ByVal lngPeriod As Long)
DoEvents
Sleep lngPeriod
End Sub

aloy78
10-03-2011, 08:04 PM
Hi gmaxey,

Yup. It works. Was wondering why it stop at the end of the first page initally. But I managed to overcome that by changing the view layout :)

However, how do I stop the script from any given point in time. Have to close the document to stop it.

Also is there a way to make the scroll to go more gently, rather than like a "jerking action". Hehehe :) If it's possible I mean. But lovely script I would say.

gmaxey
10-04-2011, 09:16 AM
Add a stop procedure.

Only by changing the "5s" to "1s" and scrolling 1 line every 1 second.

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private bStop As Boolean
Sub StopScroll()
bStop = True
End Sub
Sub StartScroll()
Dim i As Long
Dim j As Long
Dim oPage As Page
Selection.HomeKey wdStory
bStop = False
For Each oPage In ActiveDocument.ActiveWindow.ActivePane.Pages
i = i + oPage.Rectangles(1).Lines.Count
Next oPage
Do
Doze 1000 '= 1 second
ActiveDocument.ActiveWindow.SmallScroll Down:=1 'one line
j = j + 1
If bStop Then GoTo lbl_Exit
Loop Until j > i
lbl_Exit:
Exit Sub
End Sub
Sub Doze(ByVal lngPeriod As Long)
DoEvents
Sleep lngPeriod
End Sub


You might also try Word's autoscroll command. I don't know if you can make the scroll speed suit your needs or not.

aloy78
10-04-2011, 05:41 PM
Hi Greg,

Does exactly what I wanted. Thanks. :friends:

Yep, I've tried the word's autoscroll command. If I'm not mistaken it requires a mouse to initiate the scroll right? The problem with it is, I always can't seems to get the timing/speed I wanted. That's why I would prefer the ol' keyboard assigned command where I could set to the speed I want.

By the way, would you care to explain what some of these codes mean and does. I only know what some does :)

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private bStop As Boolean
Sub StopScroll() ' I guess this initate the stop *obviously :)
bStop = True
End Sub
Sub StartScroll()
Dim i As Long
Dim j As Long
Dim oPage As Page ' what does the following here means?
Selection.HomeKey wdStory
bStop = False
For Each oPage In ActiveDocument.ActiveWindow.ActivePane.Pages
i = i + oPage.Rectangles(1).Lines.Count
Next oPage ' I guess this means go on to the next page ??
Do
Doze 1000 '= 1 second
ActiveDocument.ActiveWindow.SmallScroll Down:=1 'one line
j = j + 1
If bStop Then Goto lbl_Exit ' I guess this stop the scroll?
Loop Until j > i
lbl_Exit:
Exit Sub
End Sub
Sub Doze(ByVal lngPeriod As Long) ' what does this code here do?
DoEvents
Sleep lngPeriod
End Sub

gmaxey
10-04-2011, 06:06 PM
If one line at a time is what you were after then this might look at least more elegant:

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private bStop As Boolean
Sub StopScroll()
bStop = True
End Sub
Sub StartScroll()
Dim i As Long
Dim oPage As Page
bStop = False
For Each oPage In ActiveDocument.ActiveWindow.ActivePane.Pages
For i = 1 To oPage.Rectangles(1).Lines.Count
oPage.Rectangles(1).Lines(i).Range.Select
Doze 50 '= 1 second
DoEvents
ActiveDocument.ActiveWindow.SmallScroll Down:=1 'one line
If bStop Then GoTo lbl_Exit
Next i
Next oPage
lbl_Exit:
Exit Sub
End Sub
Sub Doze(ByVal lngPeriod As Long)
DoEvents
Sleep lngPeriod
End Sub


As for what the code does. Well sometime I just write it without fully understanding it.

Dim oPage as Page
is just anohter declaration statement. Look up ActiveWindow, Pane, ActivePane, Pages, Rectangles and Lines in the Word Object model reference. What you read there will go further in explaining than I can.

"Doze" is just a local name that I have given a procedure called by the main StrartScroll procedure. It takes a long parameter "lngPeriod" which defines the wait perion. "Sleep" is the name of the Windows API called by Doze. That is really all I know.

aloy78
10-06-2011, 08:35 PM
Hi Greg,

that code doesn't work. It gave me an error message :(
It's okay, I'm content with the previous code.