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
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
After opening multiple forms (or perhaps after opening each form) try adding the line of code:
Code:
DoEvents
 
Upvote 0
Thanks for replying Chuck.

I would like this to happen automatically not have to do a process for each form as the forms should open with no captions.

As I use up to 6 forms at any given time and very frequently (some are toolbars, some are forms with worksheet info etc...), I want them to automatically work.
 
Upvote 0
Jaye7, The DoEvents command is just a sort of system flush to run all code "that should have already been run" up to that point. It should not intefere with anything automated.

Would there be anything wrong with putting DoEvents right after anywhere you have "UserForm1.Show" for instance to give it a try?

As I am assuming you would not be doing an additional process by adding DoEvents. Because loading and then showing any UserForm is a process by itself, which it sounds like did not finish its process by removing the caption? Perhaps DoEvents would finish that process.
 
Upvote 0
Thanks for replying Chuck.

I would like this to happen automatically not have to do a process for each form as the forms should open with no captions.

As I use up to 6 forms at any given time and very frequently (some are toolbars, some are forms with worksheet info etc...), I want them to automatically work.

What year excel are you using?

I am not sure what you mean by "...some are toolbars..." If you mean you are using userforms as pseudo toolbars, if you are using prior to 2007, I would look at commandbars.

Anyways, I am awfully iffy on API, but GetForegroundWindow may not be the thing to use.

As mentioned, my knowledge of API is very rudimentary, so in a junk copy of your wb try:

In a Standard Module:
Rich (BB code):
Option Explicit
    
Public Declare Function FindWindow Lib "user32" _
                        Alias "FindWindowA" (ByVal lpClassName As String, _
                                             ByVal lpWindowName As String _
                                             ) As Long
    
' 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
    
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
    
Function 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 Function
    
Function 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 Function
    
'Example of Restoring Excel's Caption
'Sub Restore_Windows_Caption()
    'RestoreCaption Application.hwnd
'End Sub
    
Sub StartForms()
    UserForm1.Show
    UserForm2.Show
End Sub

In each Userform's module:
Rich (BB code):
Option Explicit
 
Private Sub UserForm_Activate()
 
'    RemoveCaption GetForegroundWindow
End Sub
 
Private Sub CommandButton1_Click()
    Unload Me
End Sub
 
Private Sub UserForm_Initialize()
Dim lHwnd As Long
 
    lHwnd = FindWindow(vbNullString, Me.Caption)
    RemoveCaption lHwnd
End Sub

For the example, I created two userforms, setting the showmodal property to false for each and placing a commandbutton on each (lest we not be able to dismiss the form).

To use FindWindow, you will want to ensure that the caption of ea form is unique. It does not matter that the titlebar is hidden, the caption still exists, and FindWindow uses this to ID the window.

Hope that helps,

Mark
 
Last edited:
Upvote 0
Mark,

Your code is exactly what I was after, it works brilliantly, thanks very much.:):):)
 
Upvote 0
Mark,

Just one more question,

If I wanted to restore ALL of the 6 forms where I removed the captions, what code would I need to restore them using one script for ALL rather than having to click on a restore command for each form.

i.e. I have 6 forms named.

Userform1 - userform6, all without captions.
 
Upvote 0
In the Standard Module where we stuck the API Functions, try adding:
Rich (BB code):
Sub RestoreTitleBars()
Dim i As Long
Dim hWnd As Long
    
    For i = 0 To UserForms.Count - 1
        hWnd = FindWindow(vbNullString, UserForms(i).Caption)
        RestoreCaption hWnd
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,575
Members
452,652
Latest member
eduedu

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