Controlling location of message box

L

Larry

In Word 97, is there any way to control where a message box appears? I
have a macro in which the message box often blocks the text that the
macro selected and that is being queried about.

I don't see anything about this in vba help on message boxes.

Thanks,
Larry
 
H

Helmut Weber

Hi Larry,

have a look at this impressive code:

'Author: © Copyright 2001 Pacific Database Pty Limited
' Graham R Seach (e-mail address removed)
' Phone: +61 2 9872 9594 Fax: +61 2 9872 9593
'
' Adapted from Randy Birch's code for creating and
' displaying a custom MsgBox with user-defined button
' text: "Modifying a Message Box with SetWindowsHookEx"
'
' You may freely use and distribute this code
' with any applications you may develop, on the
' condition that the copyright notice remains
' unchanged, and intact as part of the code. You
' may not publish this code in any form without
' the express written permission of the copyright
' holder.
'
'Description: This function creates a custom MsgBox and,
' displays it at a user-defined position.
'
'Calling convention: MoveMsgBox "Message", "Title",
vbOKOnly+vbInformation,
lLeft, lTop
'
'Inputs: sMsg: The message to be displayed
' sTitle: The MsgBox title to be displayed
' lFlagsL The MsgBox flags (vbOKOnly, etc)
' lLeft: The Left position to display the MsgBox at
' lTop: The Top position to display the MsgBox at
'
'Outputs: None.


Private Declare Function GetDesktopWindow Lib "user32" () As Long


Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long


Private Declare Function GetWindowLong Lib "user32" Alias
"GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long


Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" _
(ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As
String,
ByVal wType As Long) 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 UnhookWindowsHookEx Lib "user32" (ByVal hHook
As
Long) As Long


Private Declare Function MoveWindow Lib "user32" _
(ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, _
ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As
Long) As
Long


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long



Private Declare Function GetWindowPlacement Lib "user32" _
(ByVal hwnd As Long, lpwndp1 As WINDOWPLACEMENT) As Long


'VBnet-defined control ID for the message prompt
Private Const IDPROMPT = &HFFFF&


'Miscellaneous constants
Private Const WH_CBT = 5
Private Const GWL_HINSTANCE = (-6)
Private Const HCBT_ACTIVATE = 5


'UDT for passing data through the hook
Private Type MSGBOX_HOOK_PARAMS
hwndOwner As Long
hHook As Long
End Type


'We need this declared at module level as it is used in the call and
the
hook proc
Private MSGHOOK As MSGBOX_HOOK_PARAMS


Private Type POINTAPI
x As Long
y As Long
End Type


Private Type rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type


Private Type WINDOWPLACEMENT
Length As Long
flags As Long
showCmd As Long
ptMinPosition As POINTAPI
ptMaxPosition As POINTAPI
rcNormalPosition As rect
End Type


'Needed in MsgBoxHookProc
Private mLeft As Long 'The Left position of the msgbox
Private mTop As Long 'The Top position of the msgbox
Private sTitle2Find As String 'The title of the msgbox


Public Sub MoveMsgBox(sMsg As String, sTitle As String, lFlags As
VbMsgBoxStyle, lLeft As Long, lTop As Long)
Dim hInstance As Long
Dim hThreadId As Long
Dim hwndOwner As Long
Dim lWidth As Long
Dim lHeight As Long


mLeft = lLeft
mTop = lTop
sTitle2Find = sTitle


'Setup the hook.
hInstance = GetWindowLong(0, GWL_HINSTANCE)
hThreadId = GetCurrentThreadId()
hwndOwner = GetDesktopWindow()


'Setup the MSGBOX_HOOK_PARAMS values.
'By specifying a Windows hook as one of the params, we can
intercept
messages
'sent by Windows and thereby manipulate the dialog.
With MSGHOOK
.hwndOwner = hwndOwner
.hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc,
hInstance, hThreadId)
End With


Debug.Print MessageBox(hwndOwner, sMsg, sTitle, lFlags)
End Sub


Public Function MsgBoxHookProc(ByVal uMsg As Long, ByVal wParam As
Long,
ByVal lParam As Long) As Long
'When the message box is about to be shown, we'll move it
If uMsg = HCBT_ACTIVATE Then
Dim mWidth As Long
Dim mHeight As Long
Dim wp As WINDOWPLACEMENT
Dim hwnd As Long
Dim retVal As Long


'Get the message box's handle
hwnd = FindWindow(vbNullString, sTitle2Find)
'Get the message box's width and height
retVal = GetWindowPlacement(hwnd, wp)
mWidth = wp.rcNormalPosition.Right - wp.rcNormalPosition.Left
mHeight = wp.rcNormalPosition.Bottom - wp.rcNormalPosition.Top
'Move the message box
Debug.Print MoveWindow(hwnd, mLeft, mTop, mWidth, mHeight,
True)


'We're done with the dialog, so release the hook.
UnhookWindowsHookEx MSGHOOK.hHook
End If


'Return False to let normal processing continue.
MsgBoxHookProc = False
End Function

http://groups.google.de/group/micro...rosoft.public.*&rnum=4&hl=de#eaf062ee804c3458


It's working for me.
 
L

Larry

Thanks, Helmut. That certainly is impressive.

Larry



Hi Larry,

have a look at this impressive code:

'Author: © Copyright 2001 Pacific Database Pty Limited
' Graham R Seach (e-mail address removed)
' Phone: +61 2 9872 9594 Fax: +61 2 9872 9593
'
' Adapted from Randy Birch's code for creating and
' displaying a custom MsgBox with user-defined button
' text: "Modifying a Message Box with SetWindowsHookEx"
'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top