Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As POINTAPI) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type POINTAPI: X As Long: Y As Long: End Type
Private Type RECT: Left As Long: Top As Long: Right As Long: Bottom As Long: End Type
Private Sub UserForm_Initialize()
Dim hdc&, TextSize As POINTAPI, Cx&, R As RECT
hdc = GetDC(FindWindow(vbNullString, Me.Caption)): GetWindowRect hwnd, R
GetTextExtentPoint32 hdc, Me.Caption, Len(Me.Caption), TextSize
Cx = (R.Right + R.Left + TextSize.X) / 2
Do While TextSize.X < Cx
Me.Caption = " " & Me.Caption
GetTextExtentPoint32 hdc, Me.Caption, Len(Me.Caption), TextSize
Loop
End Sub