I'm trying to set up a userform that will appear, remain for 10 seconds, and then close automatically.
I have done this before in Excel, using the OnTime
method:
Sub Example()
Application.OnTime EarliestTime:=Now + TimeValue("00:00:10"), _
Procedure:="Hide_userform2"
UserForm2.Show
End Sub
Sub Hide_userform2()
UserForm2.Hide
End Sub
However, because Outlook doesn't recognise the OnTime
method, I have been trying to use the Timer
statement:
Sub example2()
Strt = Timer
Do While Timer < Strt + 10
UserForm2.Show
Loop
UserForm2.Hide
End Sub
The problem with this is that when the user form opens, the macro (including the loop) pauses until the UserForm is manually closed...
Any help on a workaround for this would be much appreciated.
Cheers!
I ran some tests and the problem seems to be that once you show the User Form, it takes control and does not return it to example2()
.
What seems to work is if you put the following code in your form's Acitvate
sub, it will hide correctly. This may not be exactly what you want, you may be using the form for other things and this process will mess it up, but it gets you in the right direction.
Private Sub UserForm_Activate()
Strt = Timer
Do While Timer < Strt + 10
DoEvents 'please read linked documentation on this
Loop
UserForm2.Hide
End Sub
Using DoEvents
will make sure the form shows correctly but may have some unwanted side effects. Make sure to read this Microsoft article and a blog post by Jeff Atwood is an interesting read though not necessarily about VBA.