Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to align text in message box in vb or vba?

Is there any way to align text into the center in msgbox in VB or VBA? Does VB have any functionality to do the same?

like image 939
Pratik Gujarathi Avatar asked Feb 03 '12 15:02

Pratik Gujarathi


People also ask

How do I align text in Excel VBA?

To visually specify the alignment of text inside of one or more cells, give focus to the cell or select the cells. Then, in the Alignment section of the Home tab of the Ribbon, click the desired alignment button.

How do I use MsgBox in VBA?

A MsgBox is nothing but a dialog box that you can use to inform your users by showing a custom message or get some basic inputs (such as Yes/No or OK/Cancel). While the MsgBox dialog box is displayed, your VBA code is halted. You need to click any of the buttons in the MsgBox to run the remaining VBA code.

What VBA method is used to generate a message box?

The VBA MsgBox function is used to display messages to the user in the form of a message box. We can configure the message box to provide the user with a number of different buttons such as Yes, No, Ok, Retry, Abort, Ignore and Cancel. The MsgBox function will then return the button that was clicked.


4 Answers

No. The MsgBox() function is simply a wrapper for the Windows MessageBox() function and as such has no stylistic control over the dialog beyond the icon.

If you want to change it any further than this, you will need to create your own window and show that instead.

On Windows Vista+ you can use TaskDialogs that allow a lot more control.

like image 136
Deanna Avatar answered Oct 24 '22 06:10

Deanna


no, but you can cheat by using spaces.

msgbox("                your message")
like image 27
Omar Luján Avatar answered Oct 24 '22 07:10

Omar Luján


VBA

Some notes: http://access.mvps.org/access/bugs/bugs0035.htm AND http://www.tek-tips.com/viewthread.cfm?qid=435428 However, it is not so difficult to build your own message box, which solves all your problems.

like image 37
Fionnuala Avatar answered Oct 24 '22 06:10

Fionnuala


When you are building your strings you could pad them at the beginning and end with spaces to achieve a target length. If you're using excel the worksheet function rept is handy for this.

function pad_n_center(byval mystring as string, lenmax as integer) as string
    dim pad_by as integer
    dim pad as string
    pad_by = (lenmax - len(mystring))/2
    'some more code to finesse that?
    pad = worksheetfunction.rept(" ",pad_by)
    pad_n_center = pad & mystring & pad
end function

As mentioned before if the msgbox still doesn't look good you can use textbox shape object (or other objects) to get the desired effect.

like image 27
klausnrooster Avatar answered Oct 24 '22 07:10

klausnrooster