6

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?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Pratik Gujarathi
  • 748
  • 10
  • 22
  • 40

5 Answers5

8

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.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • TaskDialogs have been around since Vista. Keep in mind how small a change Win7 really is. – Bob77 Feb 03 '12 at 18:37
  • Ah, I forgot when they were introduced. My answer's been updated to suit. – Deanna Feb 04 '12 at 00:26
  • I think the absolute "No" here is incorrect. VB itself obviously does not support this directly but I would think a workaround such as https://stackoverflow.com/a/10760568/3195477 would work equally well in VB6. – StayOnTarget May 24 '23 at 14:59
3

no, but you can cheat by using spaces.

msgbox("                your message")
1

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.

Fionnuala
  • 90,370
  • 7
  • 114
  • 152
1

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.

klausnrooster
  • 520
  • 3
  • 13
0

you probably can use a combinatrion vbtab and controlchars.CrLf in the message box.

Serch
  • 1
  • Please add more description to the answer on how to do this. Probably adding some images of successful attempt would help improving the answer. – Timmy Chan Jul 05 '23 at 03:46