Can you format MsgBox text?

TAPS_MikeDion

Well-known Member
Joined
Aug 14, 2009
Messages
622
Office Version
  1. 2011
Platform
  1. MacOS
Hi all,
I'm sure there must be a way to dimension a string and format it for use in a MsgBox, but I have no clue how to do the initial formatting; any help would be GREATLY appreciated.

FYI - something along the lines of...

Dim S as String
S = "This part of the string is bold and italicized, and this part is not"
MsgBox S, vbInformation, "My Title"

Thanks,
Mike
P.S. If this actually is possible, can you also change the color of the text?
 
First, I wanted to thank everyone again for all of the help.

Peter, I tried ktMsgBox and it actually did do what I needed. It also has many cools features that I may wish to use in the future, so thank you for the link.

-Mike
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hello Jaafar,

Love your MsgBox flexibility ... !!!

Is there a simple way to adjust the MsgBox width ... if

within CreateFont procedure ... lfHeight is set to 32 and lfWidth is set to 10 ...

Thanks in advance for your help ...

This got me curious so i started experimenting with some window subclassing and i did manage to format (Bold) a portion of the text of a standard MsgBox .
 
Last edited by a moderator:
Upvote 0
@James

Do you mean adjusting the size of the Msgbox Prompt Text or the actual Msgbox itself ?

Regards.
 
Last edited:
Upvote 0
@ Jaafar Tribak

Actually thanks to your ' Create Font ' macro ...

the Text size inside the MsgBox can be adjusted ... (lfHeight is set to 32 and lfWidth is set to 10)

What would be great is to have the actual size of the MsgBox itself to auto-adjust to its Text Contents ...

Hope this clarifies

Best Regards
 
Upvote 0
@James006

I see what you mean.. Having the size of the Msgbox dynamically adjust itself according to the size of the Prompt Text would be nice and coding it would certainly be a good learning exercise.

Not sure if it can be done but I'll try experimenting with it and see if anything interesting comes up.

Regards.
 
Upvote 0
@ Jaafar Tribak

Thanks a lot for taking a look at this issue ...

However if you consider it as too complex ... please do not invest too much your time on this question... :smile:
 
Upvote 0
Hi James006

I hust got around to writing this code which allows the user to set the font attributes (name, size, italic, bold etc) of the standard vba Msgbox and have the size of the Msgbox auto-adjusts automatically.

Unfortunately, I had to use the SystemParametersInfo API to temporarly change the System NONCLIENTMETRICS which would be used with caution plus It doesn't resize the Msgbox Icon if there is one.

The ideal solution would be not to change the system non-client metrics. Instead, one would subclass the Msgbox which I have tried doing but with no sucess so far.

Workbook demo



Any way, here is the code in a Standard Module :
Code:
Option Explicit

Private Type LOGFONT
    lfHeight As Long
    lfWidth As Long
    lfEscapement As Long
    lfOrientation As Long
    lfWeight As Long
    lfItalic As String * 1
    lfUnderline As String * 1
    lfStrikeOut As String * 1
    lfCharSet As String * 1
    lfOutPrecision As String * 1
    lfClipPrecision As String * 1
    lfQuality As String * 1
    lfPitchAndFamily As String * 1
    lfFaceName As String * 32
End Type

Private Type NONCLIENTMETRICS
    cbSize As Long
    iBorderWidth As Long
    iScrollWidth As Long
    iScrollHeight As Long
    iCaptionWidth As Long
    iCaptionHeight As Long
    lfCaptionFont As LOGFONT
    iSMCaptionWidth As Long
    iSMCaptionHeight As Long
    lfSMCaptionFont As LOGFONT
    iMenuWidth As Long
    iMenuHeight As Long
    lfMenuFont As LOGFONT
    lfStatusFont As LOGFONT
    lfMessageFont As LOGFONT
End Type

[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
    Private Declare PtrSafe Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As LongPtr
    Private Declare PtrSafe Function DeleteObject Lib "gdi32" (ByVal hObject As LongPtr) As Long
    Private hFont As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Private Declare PtrSafe Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
    Private Declare PtrSafe Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
    Private Declare PtrSafe Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private hFont As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

Private Const SPI_GETNONCLIENTMETRICS = 41
Private Const SPI_SETNONCLIENTMETRICS = 42
Private Const SPIF_UPDATEINIFILE = &H1


Sub Test_MsgBoxEx()

    MsgBoxEx Prompt:="Hello World !", Buttons:=vbInformation + vbOKCancel, _
             FontName:="Bradley Hand ITC", FontSize:=25, FontBold:=True, FontItalic:=True

End Sub

Sub Test_Standard_MsgBox()

    MsgBox Prompt:="Hello World !", Buttons:=vbInformation + vbOKCancel

End Sub


Private Function MsgBoxEx( _
    ByVal Prompt As String, _
    Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, _
    Optional ByVal Title As String, _
    Optional ByVal HelpFile As String, _
    Optional ByVal Context As Long, _
    Optional ByVal FontName As String, _
    Optional ByVal FontSize As Single, _
    Optional ByVal FontBold As Boolean, _
    Optional ByVal FontItalic As Boolean, _
    Optional ByVal FontUnderline As Boolean, _
    Optional ByVal FontStrikeOut As Boolean _
) As VbMsgBoxResult

    Dim tDefNCMetrics As NONCLIENTMETRICS, tNCMetrics As NONCLIENTMETRICS, tLogFont As LOGFONT

    On Error GoTo ErrHandler

    tNCMetrics.cbSize = Len(tNCMetrics)
    Call SystemParametersInfo(SPI_GETNONCLIENTMETRICS, Len(tNCMetrics), tNCMetrics, 0)
    tDefNCMetrics = tNCMetrics
    
    [B][COLOR=#008000]'Safety step : Save to text file In case an error occurs and the default NONCLIENTMETRICS are lost.
    'To recover the default NONCLIENTMETRICS, run the 'RestoreDefaultNCMetrics' routine located below.[/COLOR][/B]
    Open ThisWorkbook.Path & Application.PathSeparator & "DefNCMetrics.txt" For Binary As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] 
        Put [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] , , tDefNCMetrics
    Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] 
    
    With tLogFont
        .lfFaceName = IIf(Len(FontName), FontName & Chr$(0), tDefNCMetrics.lfMessageFont.lfFaceName & Chr$(0))
        .lfHeight = IIf(FontSize, -FontSize, tDefNCMetrics.lfMessageFont.lfHeight)
        .lfWeight = IIf(FontBold, 900, tDefNCMetrics.lfMessageFont.lfWeight)
        .lfItalic = IIf(FontItalic, True, tDefNCMetrics.lfMessageFont.lfItalic)
        .lfUnderline = IIf(FontUnderline, True, tDefNCMetrics.lfMessageFont.lfUnderline)
        .lfStrikeOut = IIf(FontStrikeOut, True, tDefNCMetrics.lfMessageFont.lfStrikeOut)
    End With

    hFont = CreateFontIndirect(tLogFont)
    tNCMetrics.lfMessageFont = tLogFont
    Call SystemParametersInfo(SPI_SETNONCLIENTMETRICS, Len(tNCMetrics), tNCMetrics, SPIF_UPDATEINIFILE)

    MsgBox Prompt, Buttons, IIf(Len(Title), Title, Application.Name), HelpFile, Context

ErrHandler:

    DeleteObject hFont
    Call SystemParametersInfo(SPI_SETNONCLIENTMETRICS, Len(tDefNCMetrics), tDefNCMetrics, SPIF_UPDATEINIFILE)
    
    If Err.Number = 75 Then
        MsgBox "Oops!" & vbCr & vbCr & "This workbook must be saved to disk" & vbCr & "before running the code.", vbInformation
    End If
End Function


[B][COLOR=#008000]'Run this macro only if the default NONCLIENTMETRICS are accidently lost !!![/COLOR][/B]
Private Sub RestoreDefaultNCMetrics()

    Dim tDefNCMetrics As NONCLIENTMETRICS
    
    Open ThisWorkbook.Path & Application.PathSeparator & "DefNCMetrics.txt" For Binary As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] 
        Get [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] , , tDefNCMetrics
    Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1"]#1[/URL] 
    Call SystemParametersInfo(SPI_SETNONCLIENTMETRICS, Len(tDefNCMetrics), tDefNCMetrics, SPIF_UPDATEINIFILE)
End Sub
 
Last edited:
Upvote 0
Stupid me !

I forgot to remove the PtrSafe Keyword from the 32 bit API declarations : (The rest is all fine)

The correct API declaration should be :
Code:
#If VBA7 Then
    Private Declare PtrSafe Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
    Private Declare PtrSafe Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As LongPtr
    Private Declare PtrSafe Function DeleteObject Lib "gdi32" (ByVal hObject As LongPtr) As Long
    Private hFont As LongPtr
#Else
    Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
    Private Declare Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private hFont As Long
#End If
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,150
Messages
6,170,377
Members
452,322
Latest member
CrimsonCoure

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