PDA

View Full Version : [SOLVED:] Formatting inside message box



stevetalaga
03-02-2014, 08:43 PM
Is it possible to format text within a message box?

I would like to have some of the message text in bold - I can't see where this could be done.
I would also like to display the text on 2 lines rather than 1 long line. I have tried this but it doesn't work:


Dim strMsg1 As String
Dim strMsg2 As String
Dim strStyle As String
Dim strTitle As String
Dim strResponse As String

strMsg1 = "Line 1 of message text"
strMsg2 = "Line 2 of message text"
strStyle = vbYesNo + vbQuestion + vbDefaultButton2
strTitle = "2 Lines of Message"

strResponse = MsgBox(strMsg1 & wdCRLF & strMsg2, strStyle, strTitle)



Thanks

fumei
03-02-2014, 08:53 PM
messagebox strings are one string. That said, you can certainly make it LOOK like two lines by using a vbCrLF. I do not know where you got wdCRLF, but that is a NUMBER (= 0).

So either use:
strMsg1 = "Line 1 of message text" & vbCrLf

OR

strResponse = MsgBox(strMsg1 & vbCRLF & strMsg2, strStyle, strTitle

Also, not sure what you are trying to do with strResponse as a STRING. Messagebox returns, again, a NUMBER.

And no, you can not format text in messageboxes. If you really need to do this, use a userform.

stevetalaga
03-02-2014, 09:07 PM
Thanks!

The vbCRLF works great. I found the wdCRLF in "Word Help" but the context was not quite what I needed and yes, it did stick a "0" in between the two text strings rather than a new line.

The strResponse bit is used as follows:


strResponse = MsgBox(strMsg1 & vbCrLf & strMsg2, strStyle, strTitle)
If strResponse = vbNo Then
Exit Sub
End If


I will explore userforms for the formatting.

Thanks again.

fumei
03-03-2014, 05:21 AM
You do not need strResponse at all.

If MsgBox(strMsg1 & vbCrLf & strMsg2, strStyle, strTitle) = vbNo Then
Exit Sub
End If

Although you should have something to tell VBA what to do if there us a Yes.

If MsgBox(strMsg1 & vbCrLf & strMsg2, strStyle, strTitle) = vbNo Then
Exit Sub
Else
' whatever Yes is supposed to do
End If


The point though is that you declare strResponse as a STRING; and MessageBox returns a number...not a string. If you really want a variable to hold the returned value of a messagebox - and the above code shows that you do not, at least for what you are doing - it should be declared as a number.

Dim lngMsgBox as Long

stevetalaga
03-03-2014, 09:49 AM
Thanks again - Your code looks simpler. I will use that.