Creating a  Splash Screen with a UserForm

Article contributed by Ibby

A UserForm can be used as a splash screen – a dialog that unloads itself after a specified amount of time. The following example shows how to create a UserForm that acts as a splash screen when you create a new document based on a template. In this case, the UserForm is displayed for 5 seconds, then it unloads itself.

In the UserForm module:

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

Private Sub UserForm_Activate()
     DoEvents
    ' Set the display time in milliseconds
     Sleep 5000
     Unload Me
End Sub

In a code Module:

Public Sub AutoNew()
    UserForm1.Show
End Sub

If you want this to work on the Mac as well

The Sleep API call does not work on the Mac, but you can use DoEvents to get Mac compatibility, as follows:

In the UserForm Module:

Private Sub UserForm_Activate()

Dim PauseTime As Single, Start As Single, Finish As Single, TotalTime As Single

PauseTime = 5    ' Set duration.
Start = Timer    ' Set start time.
Do While Timer < Start + PauseTime
    DoEvents    ' Yield to other processes.
Loop
Finish = Timer    ' Set end time.
TotalTime = Finish - Start    ' Calculate total time.
Unload Me

End Sub


Private Sub UserForm_QueryClose(Cancel As Long, CloseMode As Long)
    If CloseMode = vbFormControlMenu Then Cancel = True
End Sub

(Note that the above separator line is included to aid readability; but you will need to delete it if you paste the code into your own project).

In a code module:

Public Sub AutoNew()
    UserForm1.Show
End Sub
  

The Sleep API call is much less resource-hungry than DoEvents, so if you don't need Mac-compatibility, using the API call is better.

If you want the user to be able to cancel out of the splash screen

The following works on both Windows and the Mac:

On your UserForm, include a Cancel button, i.e. a CommandButton with its Cancel property set to True (it doesn't need to have a caption, but set its Name property to be cmdCancel).

Put an image control on top of the Cancel button, so that the button is obscured (but don't set the Cancel button's visibility to False, or else pressing Escape won't invoke it). Note that the image control does not necessarily need to contain an image, although if your splash screen contains an image anyway, you can, of course, use that image. You can bring the image control in front of the button using Format + Order.

In the UserForm Module:

Option Explicit
Dim
FormClosed As Boolean

Private Sub UserForm_Activate()
    FormClosed = False
    Dim StartTime As Single
    StartTime = Timer
    Do While Timer < StartTime + 5
        If FormClosed Then Exit Sub
        DoEvents
    Loop
    Unload Me
End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    FormClosed = True
End Sub


Private Sub cmdCancel_Click()
    Unload Me
    FormClosed = True
End Sub
 

In a code Module:

Public Sub AutoNew()
    UserForm1.Show
End Sub

See also:
Running a macro automatically when a document is created, opened or closed
Running a macro automatically when Word starts or quits