Remove UserForm Caption

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,069
I use the following code to remove captions, however if my script opens multiple forms then it only removes the caption from the last opened form, utill I click on the others and then it removes them.

Can someone please help with a code to remove each caption when the forms open.

My remove caption script is already in the form module, as per note at the end.


Code:
'Option Private Module
'Returns the Window Handle of the Window
'that is accepting User input.
 Public Declare Function GetForegroundWindow _
   Lib "user32.dll" () As Long
 Private Declare Function GetWindowLong _
  Lib "user32.dll" _
    Alias "GetWindowLongA" _
     (ByVal hwnd As Long, _
      ByVal nIndex As Long) As Long
 
 Private Declare Function SetWindowLong _
  Lib "user32.dll" _
    Alias "SetWindowLongA" _
     (ByVal hwnd As Long, _
      ByVal nIndex As Long, _
      ByVal dwNewLong As Long) As Long
'Redraw the Icons on the Window's Title Bar
 Private Declare Function DrawMenuBar _
   Lib "user32.dll" _
    (ByVal hwnd As Long) As Long
 Private Const GWL_STYLE As Long = (-16)
 Private Const WS_CAPTION = &HC00000
Sub RemoveCaption(ByVal hwnd As Long)
 
  Dim BitMask As Long
  Dim WindowStyle As Long
 
    WindowStyle = GetWindowLong(hwnd, GWL_STYLE)
    BitMask = WindowStyle And (Not WS_CAPTION)
    Call SetWindowLong(hwnd, GWL_STYLE, BitMask)
    Call DrawMenuBar(hwnd)
 
End Sub
Sub RestoreCaption(ByVal hwnd As Long)
 
  Dim BitMask As Long
  Dim WindowStyle As Long
 
    WindowStyle = GetWindowLong(hwnd, GWL_STYLE)
    BitMask = WindowStyle Or WS_CAPTION
    Call SetWindowLong(hwnd, GWL_STYLE, BitMask)
    Call DrawMenuBar(hwnd)
 
End Sub
 
'Example of Restoring Excel's Caption
'Sub Restore_Windows_Caption()
    'RestoreCaption Application.hwnd
'End Sub

NOTE: I already have the remove caption in the form code as follows

Code:
 Private Sub UserForm_Activate()
RemoveCaption GetForegroundWindow
end sub
 
I modified the code to the following as the restorecaption section was locking up, however it didn't restore the captions.

Code:
Sub RestoreTitleBars()
Dim i As Long
Dim hWnd As Long
    
    For i = 0 To UserForms.Count - 1
        hWnd = FindWindow(vbNullString, UserForms(i).Caption)
        [COLOR=red]RestoreCaption Application.hWnd
[/COLOR]    Next
End Sub
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,223,952
Messages
6,175,589
Members
452,653
Latest member
craigje92

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top