Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. 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, 2013, 2016, 2019, and Excel in Office 365. 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

Saving in Multiple Locations

Need to save a workbook in more than one location? Here's a handy macro that can save your workbook in lots of different ...

Discover More

Determining Page Layout

Getting your document onto paper is the whole purpose of word processing. Here are some concepts that are important when ...

Discover More

Getting Rid of Modify Style Message

When you apply styles to a paragraph, you may periodically see a message asking if you want to reapply the style or ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Updating Automatically when Opening Under Macro Control

If your workbook contains links, you are normally given the opportunity to update those links when you open the workbook. ...

Discover More

Understanding the Select Case Structure

One of the powerful programming structures available in VBA is the Select Case structure. This tip explains how you can ...

Discover More

Stopping a Checked Box from being Unchecked

When creating user forms for use in Excel, you are provided with a range of controls you can add, including check boxes. ...

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 2 + 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.