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

7

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?

Leavings answered 3/2, 2012 at 15:24 Comment(0)
P
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.

Precritical answered 3/2, 2012 at 17:18 Comment(4)
TaskDialogs have been around since Vista. Keep in mind how small a change Win7 really is.Tetrabasic
Ah, I forgot when they were introduced. My answer's been updated to suit.Precritical
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://mcmap.net/q/328294/-winforms-how-can-i-make-messagebox-appear-centered-on-mainform would work equally well in VB6.Teno
@Teno that question and answer centers the dialog over the window, not the text in the dialog which is being asked here.Precritical
E
4

no, but you can cheat by using spaces.

msgbox("                your message")
Endolymph answered 18/1, 2017 at 20:40 Comment(0)
B
2

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.

Butler answered 6/2, 2012 at 1:35 Comment(2)
Not sure why you would need the right pad but pretty usefulCherian
@Cherian Padding the right could keep the box at max widthCotta
A
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.

Anfractuous answered 3/2, 2012 at 15:30 Comment(0)
H
1

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

Horick answered 27/6, 2023 at 5:14 Comment(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.Pyne
L
-1

It is easy to "center" an Application.Input box: When the box opens, with the mouse move the box where you want it to open in the future and it does just that. It opens where you last position it. At least that; what it does for me.

Liquefacient answered 5/11, 2023 at 4:34 Comment(1)
The OP is asking about MgsBox, not Application.Input. And the OP is asking about centering the text inside the MsgBox, not centering the MsgBox itself relative to the screen.Venomous
I
-2

MsgBox([Put Your Text Here], MsgBoxStyle.Information & MessageBoxOptions.RightAlign)

I Trid and Its Work

Impoverish answered 5/8 at 7:57 Comment(1)
No, you didn't try it. There are no MsgBoxStyle.Information or MessageBoxOptions.RightAlign in VBA/VB6. And in VB.NET, the expression MsgBoxStyle.Information & MessageBoxOptions.RightAlign is a string concatenation of the two numbers rather than their binary union. And even if we suspend the disbelief and assume this was C#, that would still need to be |, not &.Venomous

© 2022 - 2024 — McMap. All rights reserved.