Sometimes it is desirable to remove the entire title bar of a form. For example when displaying a so-called 'splash screen' when starting an application or when showing a form with a progress bar.In these cases, it can be nicer to use a form without a title bar.
The VBA userforms do not have an option to remove the title bar by default, but this can be achieved using the Windows API. The code for this must be put in a separate module and can then easily be called in a userform where the title bar must be removed. The code to put in the module is as follows:
Private Const GWL_STYLE = -16
Private Const WS_CAPTION = &HC00000
#If VBA7 Then
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#If Win64 Then
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#Else
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#End If
Private Declare PtrSafe Function DrawMenuBar Lib "user32" (ByVal hWnd As LongPtr) As Long
#Else
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
#End If
Public Sub HideFormTitleBar(frm As Object)
#If VBA7 Then
Dim lStyle As LongPtr, lFrmHandle As LongPtr
#Else
Dim lStyle As Long, lFrmHandle As Long
#End If
lFrmHandle = FindWindow("ThunderDFrame", frm.Caption)
lStyle = GetWindowLong(lFrmHandle, GWL_STYLE)
lStyle = lStyle And Not WS_CAPTION
SetWindowLong lFrmHandle, GWL_STYLE, lStyle
DrawMenuBar lFrmHandle
End Sub
This code works only in Windows and works in both the 32 and 64 bit version of Excel and Word. The title bar of a form can then be removed by adding the following line to the UserForm_Initialize event:
Private Sub UserForm_Initialize()
HideFormTitleBar Me
End Sub
As mentioned, this can be used for a splash screen. To do this, create a new form and enter the items that should be displayed on the splash screen, such as the logo and name of the application. Call this form frmSplash. In Excel this splash screen can be shown by the following code in the Workbook_Open event of ThisWorkbook class module:
Private Sub Workbook_Open()
ActiveWindow.Visible = False
frmSplash.Show
Windows(ThisWorkbook.Name).Visible = True
End Sub
If this splash screen should be visible for 5 seconds, put the following code in the UserForm_Activate event of the form:
Private Sub UserForm_Activate()
Application.Wait (Now + TimeValue("00:00:05"))
Unload Me
End Sub
In this way, when starting the Excel file, the splash screen is first displayed for 5 seconds, after which the Excel file becomes visible.
Questions / suggestions
Hopefully, this article helped you remove the title bar from a VBA Userform. If you have any questions about this topic or suggestions for improvement, please post a comment below.