What is the equivalent in O2K vb to "Sleep()"?

  • Thread starter Thread starter StargateFan
  • Start date Start date
S

StargateFan

Went hunting in the archives and didn't find anything there or in the
help file relating to a "sleep" function. In other scripting
languages, just to give an example, you can type in something like

Sleep(10000) 'for 10 seconds
or even just
Sleep 10000

and the macro pauses for the length of time stated. How would one do
that in O2K vb, pls? Thanks. :oD
 
Outlook VBA is not a scripting language, nor is any other variant of VBA
code. In VBA you can use the Win32 API Sleep method, just be very careful to
make sure it's not an infinite sleep since the thread still has to process
Windows messages.

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sleep 2000 ' sleep for 2000 ms = 2 seconds
 
Ken Slovak - said:
Outlook VBA is not a scripting language, nor is any other variant of VBA
code. In VBA you can use the Win32 API Sleep method, just be very careful
to make sure it's not an infinite sleep since the thread still has to
process Windows messages.

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sleep 2000 ' sleep for 2000 ms = 2 seconds

Yes, that was impressed upon me very much by the very many messages in
response to this query in the archives. But there was no alternative given.
If need be, a looping mechanism or something that would have accomplished
the end result would have been fine. But did try this, both with and
without the "Private Declare" statement and I get an error message so either
I'm doing something wrong, or the syntax is incorrect.

Here's is the script:
***************************
Private Sub Application_Startup()
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sleep 2000 ' sleep for 2000 ms = 2 seconds

strMsg = "Do you want to run the macro to expand all the folders... ?"
If MsgBox(strMsg, vbYesNo + vbQuestion, "Expand all?") = vbYes Then
ExpandAllFolders
End If
End Sub
***************************

Whether or not the "Private Declare" line has been put in, either way the
vbe is brought up with "sleep" highlighted and the error:
"Sub of Function not defined."

Thanks. :)
 
The declaration for the Sleep() API function, as with any similar API
declarations, should be placed at module level and not inside another
method. Move it to above any code procedures in that module.
 
Ken Slovak - said:
The declaration for the Sleep() API function, as with any similar API
declarations, should be placed at module level and not inside another
method. Move it to above any code procedures in that module.

Okay, though I understand the words individually, didn't understand all that
above <g>. What I did get out of that was to put it at the very top of the
"module", so I put this line which seemed to be most logical pat to put at
the very top:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
' This line is so that you can use "Sleep" in any macro.


Then I changed the macro to this:
********************************************
Private Sub Application_Startup()
Sleep 2000 ' sleep for 2000 ms = 2 seconds
strMsg = "Do you want to run the macro to expand all the folders... ?"
If MsgBox(strMsg, vbYesNo + vbQuestion, "Expand all?") = vbYes Then
ExpandAllFolders
End If
End Sub
********************************************
I even tried 5000 for 5 seconds. But it's still not behaving as it should.
The problem is that the box for this macro comes up on the heels of the
prompt to enable/disable macros. All the sleep does is delay that
enable/disable box from disappearing so that this box is _still_ being
called up on the heels of the other one's disappearance. Can we fine-tune
this to something more logical? What is needed is that _after_ the
disable/enable macros prompt _disappears_, then the above macro has a delay
before it pops up? Is this do-able? The other just isn't logical. It just
seems that the enable/disable macros prompt box has gotten hung.

Thanks much for your patience. :oD
 
As soon as you enable the macros to run the Sleep() method is called,
therefore preventing the Outlook thread from processing the message to close
that warning dialog. That's one of the reasons that people were warning
against using Sleep().

What you really need is a OnStartupComplete event, only available in COM
addins and not in the Outlook VBA project.

An alternative would be if you have a 3rd party timer control available to
you or one from Visual Studio 6. Then you can start the timer in Startup()
and set it for say 5 seconds and when the timer interrupt fires you can then
show your MsgBox. That would allow Outlook to process the message to close
that warning dialog about enabling macros while still having the timer run.

If you don't have a timer control available then you'd need to use a Win32
API timer, which is a lot more complicated to create and set up.
 
Thank you for the education. Much appreciated. Seems that Outlook makes
things difficult. Sleep works extremely well in other scripting programs
that I'm familiar with <g>. Ah well. Will just have to live without all
that and since I'm going to remove the sleep code and have the boxes pop up
one after the other. Not elegant, doesn't give one time to breath, and
might accidentally click the 2nd box too quickly, but I'll get just have to
get used to it. Better than that hanging box!

It's all just rather annoying. <sigh>

Thanks. :oD
 
Is there a possible workaround that somehow does what sleep _should_ do,
without keeping boxes from closing properly? Anything along this line:

Application.Wait Now + TimeSerial(0, 0, 3) ' 3 seconds

This is actually an Excel code kindly given to me but I added it to the
beginning of the opening macro in a workbook and the enable macros box
closed and 3 seconds passed before the message box popped up so this _does_
work in Excel. The hour glass kept working during that 3 second wait but
that doesn't matter. The previous box closed immediately as it should and
didn't seem to hang, so that worked well.

Is there an Outlook equivalent?? I did try that code in the O2K vbe but get
this error:
"Run-time error '438':
Object doesn't support this property or method"

************************************************************
Private Sub Application_Startup()
Application.Wait Now + TimeSerial(0, 0, 3) ' 3 seconds
strMsg = "Do you want to run the macro to expand all the folders... ?"
If MsgBox(strMsg, vbYesNo + vbQuestion, "Expand all?") = vbYes Then
ExpandAllFolders
End If
End Sub

************************************************************
Thanks. :oD
 
There's no Outlook equivalent. You would have to use a timer control or a
Win32 API timer.
 

Thank you! Will definitely check this out.

[snip]

[Copied here since websites tend to disappear sometimes ... <g>.]

*************************************
2007-12-19 05:40:58
(http://www.vboffice.net/sample.html?mnu=2&pub=6&lang=en&smp=4&cmd=showitem)
API Timer
Author: Michael Bauer Homepage
Date: 18.01.2006 Accessed: 2227

Description

A timer is useful if you want to start a function in a short interval.
Because there's no Timer control in VBA available you have to use the
Win32 API instead.

This sample shows such an API timer, and how to receive its event:
Create a standard module, here called 'modTimer' and copy the code
into it, and create a public method called 'Timer' in
'ThisOutlookSession'. That method will be called by the timer. The
timer is enabled in Application_Startup, that is when Outlook starts,
with an interval of 60000 milliseconds (= 1 minute).

Please note: you must disable the timer before Outlook quits!

Private Sub Application_Startup()
EnableTimer 60000, Me
End Sub

Public Sub Timer()
' Called from the timer
End Sub

Private Sub Application_Quit()
DisableTimer
End Sub

' <Modul: modTimer.bas>
Option Explicit
Private Declare Function SetTimer Lib "user32.dll" (ByVal hwnd As
Long, _
ByVal nIDEvent As Long, ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long

Private Declare Function KillTimer Lib "user32.dll" (ByVal hwnd As
Long, _
ByVal nIDEvent As Long) As Long

Const WM_TIMER = &H113

Private hEvent As Long
Private m_oCallback As Object

Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long _
)
If uMsg = WM_TIMER Then
m_oCallback.Timer
End If
End Sub

Public Function EnableTimer(ByVal msInterval As Long, oCallback As
Object) As Boolean
If hEvent <> 0 Then
Exit Function
End If
hEvent = SetTimer(0&, 0&, msInterval, AddressOf TimerProc)
Set m_oCallback = oCallback
EnableTimer = CBool(hEvent)
End Function

Public Function DisableTimer()
If hEvent = 0 Then
Exit Function
End If
KillTimer 0&, hEvent
hEvent = 0
End Function
' <modTimer.bas>
*************************************
 
If I want you to copy and publish my pages I will let you know! Until then,
do not do that again!

--
Best regards
Michael Bauer - MVP Outlook
Synchronize Outlook Categories:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Wed, 19 Dec 2007 05:41:50 -0500 schrieb StargateFan:

Thank you! Will definitely check this out.

[snip]

[Copied here since websites tend to disappear sometimes ... <g>.]

*************************************
2007-12-19 05:40:58
(http://www.vboffice.net/sample.html?mnu=2&pub=6&lang=en&smp=4&cmd=showitem)
API Timer
Author: Michael Bauer Homepage
Date: 18.01.2006 Accessed: 2227

Description

A timer is useful if you want to start a function in a short interval.
Because there's no Timer control in VBA available you have to use the
Win32 API instead.

This sample shows such an API timer, and how to receive its event:
Create a standard module, here called 'modTimer' and copy the code
into it, and create a public method called 'Timer' in
'ThisOutlookSession'. That method will be called by the timer. The
timer is enabled in Application_Startup, that is when Outlook starts,
with an interval of 60000 milliseconds (= 1 minute).

Please note: you must disable the timer before Outlook quits!

Private Sub Application_Startup()
EnableTimer 60000, Me
End Sub

Public Sub Timer()
' Called from the timer
End Sub

Private Sub Application_Quit()
DisableTimer
End Sub

' <Modul: modTimer.bas>
Option Explicit
Private Declare Function SetTimer Lib "user32.dll" (ByVal hwnd As
Long, _
ByVal nIDEvent As Long, ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long

Private Declare Function KillTimer Lib "user32.dll" (ByVal hwnd As
Long, _
ByVal nIDEvent As Long) As Long

Const WM_TIMER = &H113

Private hEvent As Long
Private m_oCallback As Object

Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long _
)
If uMsg = WM_TIMER Then
m_oCallback.Timer
End If
End Sub

Public Function EnableTimer(ByVal msInterval As Long, oCallback As
Object) As Boolean
If hEvent <> 0 Then
Exit Function
End If
hEvent = SetTimer(0&, 0&, msInterval, AddressOf TimerProc)
Set m_oCallback = oCallback
EnableTimer = CBool(hEvent)
End Function

Public Function DisableTimer()
If hEvent = 0 Then
Exit Function
End If
KillTimer 0&, hEvent
hEvent = 0
End Function
' <modTimer.bas>
*************************************
 
You can use it with VBA, not VBScript. Open the VBA editor with ALT+f11;
open the project explorer with ctrl+r, double click ThisOutlookSession and
copy the first three procedures into it. Then click Insert/Module and copy
the rest into the new module.

Save the project, and place the cursor into the procedure
Application_Startup and press f5 to run it (or close and open Outlook).

--
Best regards
Michael Bauer - MVP Outlook
Synchronize Outlook Categories:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Tue, 29 Jan 2008 15:36:22 -0800 schrieb soworl:
how can i use this module on the Script Editer?

Thanks,
http://www.vboffice.net/sample.html?mnu=2&pub=6&lang=en&smp=4&cmd=showitem
 
Yes, I can do it that way.
but it runs at once the oulook run.
I want to make sleep from custom form.
I design appointment form that has button control.
at once the button is clicked, make sleep 30sec and run some code.

please, advice me

Thanks,
soworl
 
Back
Top