Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Specifying Location for a Message Box.

Specifying Location for a Message Box

by Allen Wyatt
(last updated August 22, 2018)

4

Leonard wonders if, when displaying a message box in a macro, there is a way to force the box to appear at a specific location on the screen.

There is no way to do this, as the MsgBox function doesn't include any way to specify a location. Instead, Excel displays the message box centered on the screen. If you need the capability to position the box, then the easiest solution is to rely upon the InputBox function. Unlike MsgBox, InputBox includes option parameters for specifying a location. Here's an example:

sName = InputBox(Prompt:="Enter your name", XPos:=2880, YPos:=1440)

Note that you can specify both an X position and a Y position for the upper-left corner of the box. The values assigned to these parameters are measured from the top-left corner of the screen, and are specified in twips. (There are 1440 twips to an inch.)

An input box does, of course, expect the user to provide input, whereas a message box does not. If you don't want to potentially confuse your users by soliciting input when none is really needed, then you'll need to create a UserForm to simulate a message box.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10030) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Specifying Location for a Message Box.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Changing Above-the-Line Fonts

Word maintains a list of the fonts most recently used in the program. You can't modify the list, but you can turn it on ...

Discover More

Reducing the Curl in Printed Documents

Have you ever printed out a document, only to have the pages curl very badly as they come out of the printer? There's a ...

Discover More

Generating Random Strings of Characters

If you need to generate a random sequence of characters, of a fixed length, then you'll appreciate the discussion in this ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Self-Aware Macros

Sometimes it may be helpful for a macro to know exactly where it is being executed. This tip provides a way that you can ...

Discover More

Showing RGB Colors in a Cell

Excel allows you to specify the RGB (red, green, and blue) value for any color used in a cell. Here's a quick way to see ...

Discover More

Removing a Macro from a Shortcut Key

When you assign a macro to a shortcut key, you make it easy to run the macro without ever removing your hands from the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four more than 2?

2018-10-17 19:47:38

Riley Carney

In response to Bob D:

Here is the full function for setting up a window at a specific location using MsgBoxPos. It can be made safer using #if #else #end if on the declaration of .dll functions but this should work regardless.

___________________________________________________________________________________________________________________________
Code Modified by Riley Carney: https://www.linkedin.com/in/riley-carney-34141563/
Multiple online resources were used.
___________________________________________________________________________________________________________________________
Option Explicit

'Function Declaration
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long) As Long

'Variable Declaration
Private hHook As Long
Private MsgBoxCaption As String
Private msgbox_x As Long
Private msgbox_y As Long

'Constant Declaration
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Function: msgBoxPos
'Input: strPromt - a String for the text of the MsgBox
' vbButtons - a VbMsgBoxStyle based on how the MsgBox should be styled
' strTitle - a String for the title of the MsgBox
' xPos - a Long for the x-coordinate on where the MsgBox should be placed on the screen
' yPos - a Long for the y-coordinate on where the MsgBox should be placed on the screen
'Purpose: To display a message box at an (x,y) coordinate on the pixel mapping of the screen
' Note: If you want to do a point mapping you will need to create a function which finds the pixels per point of your screen
'Returns: VbMsgBoxResult : Based on user-input
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function msgBoxPos(strPromt As String, _
vbButtons As VbMsgBoxStyle, _
strTitle As String, _
xPos As Long, _
yPos As Long) As VbMsgBoxResult
msgbox_x = xPos
msgbox_y = yPos
MsgBoxCaption = strTitle
hHook = SetWindowsHookEx(WH_CBT, _
AddressOf MsgBoxHookProc, _
0, _
GetCurrentThreadId)
msgBoxPos = MsgBox(strPromt, vbButtons, strTitle)
End Function

'Private Functions
Private Function MsgBoxHookProc(ByVal lMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If lMsg = HCBT_ACTIVATE Then
If GetWinCaption(wParam) = MsgBoxCaption Then
SetWindowPos wParam, 0, msgbox_x, msgbox_y, _
0, 0, SWP_NOSIZE + SWP_NOZORDER
UnhookWindowsHookEx hHook
End If
End If
MsgBoxHookProc = False
End Function

Private Function GetWinCaption(WinHdl As Long) As String
Const BufSize As Long = 255
Dim Buffer As String * BufSize 'Use fixed-length buffer
Dim CaptionLen As Long
CaptionLen = GetWindowText(WinHdl, Buffer, BufSize)
If CaptionLen > 0 Then GetWinCaption = Left(Buffer, CaptionLen)
End Function


2018-08-23 09:05:24

Peter Atherton

Hi Bob D

I can't get it to run, error on:
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc1, hInst, Thread)

WinProc1 not defined

Regards


2018-08-22 23:52:48

Bob D


Greetings Allen,

Your post d/d 22081018 (08222018 in US speak?) regarding Positioning of Message Box refers.

Info below was published many years ago and still appears to work ok (at least with my 2007 version).

Perhaps it may be of interest.

Regards, Bob.


Option Explicit
Option Base 1

' http://support.microsoft.com/?scid=kb%3Ben-us%3B180936&x=11&y=12

Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Public Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _
ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Public Declare Function GetWindowRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long

Public Const GWL_HINSTANCE = (-6)
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOACTIVATE = &H10
Public Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5

Public hHook As Long

Sub Test_Go()
Dim hInst As Long
Dim Thread As Long
hInst = GetWindowLong(Application.hwnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc1, hInst, _
Thread)
MsgBox "This message box has been positioned at (0,0)."
End Sub


2018-08-22 03:23:05

Canmaker

A User Form has also has the great advantage that you have control over the size colour and typeface of the text - which you don't have in a message box


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.