MSGBOX Position!

The position of a messagebox in a worksheet. Realized over API. The source is a MSDN article (Source in the code). The files at the end of the article are Excelfiles of the version 2003 and 2007.

The following code belonged in "Module1"

Option Explicit
Private Declare Function UnhookWindowsHookEx Lib "user32" _
    (ByVal hHook As Long) As Long
Private Declare Function GetWindowLong Lib "user32" _
    Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex 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 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
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Const GWL_HINSTANCE = (-6)
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOACTIVATE = &H10
Private Const HCBT_ACTIVATE = 5
Private Const WH_CBT = 5
Private hHook As Long
Sub Main()
    Dim hInst As Long
    Dim Thread As Long
    hInst = GetWindowLong(Application.hwnd, GWL_HINSTANCE)
    Thread = GetCurrentThreadId()
    hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc1, hInst, _
    MsgBox "This message box has been positioned at (0,0)."
End Sub
Function WinProc1(ByVal lMsg As Long, ByVal wParam As Long, _
    ByVal lParam As Long) As Long
    If lMsg = HCBT_ACTIVATE Then
        SetWindowPos wParam, 0, 0, 0, 0, 0, _
        UnhookWindowsHookEx hHook
    End If
    WinProc1 = False
End Function

Sample 2003
Sample 2007

Beliebte Posts aus diesem Blog

Formeln - auch Array - per VBA eintragen...

Alle Dateien eines Ordners - Optional mit Unterordner

Excel -> Word in Textmarken (Bookmarks)...