Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the Font Size and Font Weight

Status
Not open for further replies.

wynnaj

Instructor
Apr 11, 2007
1
US
I have a custome built message Box in my VBA code that provides the user with some information. The proble that I have is how to I change the Font to a larger point (i.e. 12) and the weight of the font to Bold?
 
You can't format the contents of a built-in MsgBox. If you really want to do it you must create a Userform that looks like a message box and use that instead.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
>You can't format the contents of a built-in MsgBox

Well, you can - but it isn't for the faint-hearted ...
 
There are api calls to achieve this but it's a complicated thing to do if you just want to change the font..
 
> > You can't format the contents of a built-in MsgBox

> Well, you can - but it isn't for the faint-hearted ...


How? I didn't think (even with APIs) you could format MsgBox text.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
I believe you can do it with systemparametersinfo and NonClientMetrics APIs

I'm also pretty sure you can change the font by getting the hWnd of the button and setting the font via the exttextout api.

It's not simple.
It's not worthwhile.

But it can be done...


sugarflux
 
Here's my VB6 example method, adapted for VBA. Just copy this code into a module:
Code:
[blue]Option Explicit

' Necessary constants  for hooking
Private Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5

' Working variables that require global scope in hooking module
Private hHook As Long

' The API declarations we need for hooking
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

' Utility API declarations
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 GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount 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

' Necessary in VBA hosts since we generall won't have an hInstance
Public Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long

' SOme constants for messing about a bit with the messagebox display
Private Const GWL_STYLE = (-16)
Private Const ES_CENTER = &H1&
Private Const WM_SETFONT = &H30

Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2



' Our wrapper for the normal MsgBox function
' this version works in Word
Public Function vbMsgBox(Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title As String, Optional HelpFile As String, Optional Context As Long) As Long
    hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc, GetModuleHandle("winword.exe"), 0&)
    vbMsgBox = MsgBox(Prompt, Buttons, Title, HelpFile, Context)
End Function

Private Function WinProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim hwndCaption As Long
    Dim CurrentStyle As Long
    Dim ClassName As String
    
    Dim myFont As IFont
    
    Set myFont = UserForm1.Font
    
    If lMsg = HCBT_ACTIVATE Then
        hwndCaption = GetWindow(wParam, GW_CHILD)
        Do Until GetClass(hwndCaption) = "Static"
            hwndCaption = GetWindow(hwndCaption, GW_HWNDNEXT)
        Loop
        CurrentStyle = GetWindowLong(hwndCaption, GWL_STYLE)
        SetWindowLong hwndCaption, GWL_STYLE, CurrentStyle Or ES_CENTER
        SendMessage hwndCaption, WM_SETFONT, myFont.hFont, 0&
        UnhookWindowsHookEx hHook
    End If

    WinProc = False
End Function[/blue]

This particular version sets the font of the msgbox text to match the font of UserForm1 (so you'll need a UserForm1 ...). The add a command button and drop in:
Code:
[blue]Private Sub CommandButton1_Click()
    vbMsgBox "Here we go", , "Just an example"
End Sub[/blue]

 
OK. Thanks, Mike. Technically I suppose it's still a Message Box but the superimpostion of the style on top of what the MsgBox function itself does really makes it something else in my book. Nonetheless I can probably find a use for it [smile]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
The style thing was just to show other stuff you can do. Changing the font was just a SendMessage ... :)
 
(plus I'm only changing the style of a label in the msgbox, not the msgbox itself)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top