Microsoft Office Tutorials and References
In Depth Information
REMOVE THE CAPTION BAR FROM A USER FORM
REMOVE THE CAPTION BAR
FROM A USER FORM
Challenge: You want to show a user form in Excel and prevent users from
closing the form by clicking the red X close button in the corner.
Solution: You can hide the caption bar—and therefore also the red X close
button—in a user form. Doing so requires a bit of Windows API.
One problem is that no one can move the form if the caption is missing. The
last bit of code in the macro below therefore uses the MouseDown method to
allow the form to be moved.
In VBA, you select Insert Userform. Draw one button on the form (or users
won’t have any way to close the form!). Right-click the form and choose View
Code. Paste the following code into the code pane for the form:
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
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Const GWL_STYLE As Long = (-16)
Private wHandle As Long
Part
3
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim frm As Long, frmstyle As Long
If Val(Application.Version) >= 9 Then
wHandle = FindWindow("ThunderDFrame", Me.Caption)
Else
wHandle = FindWindow("ThunderXFrame", Me.Caption)
 
 
Search JabSto ::




Custom Search