View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Rech Jim Rech is offline
external usenet poster
 
Posts: 2,718
Default API Call from VBA crashing Excel

Why are you bothering to subclass a userform when you can just user the Top
and Left properties to position it (when StartupPosition is set to manual)?

--
Jim
"Philip" wrote in message
...
| Hi all,
|
| I am trying to center a Windows Common Dialog using the API call. Trouble
| is, when the code gets to 'SetWindowsHookEx' below it is crashing...
|
| CODE
| ' Set up the CBT hook to center the dialog on the screen when Windows
| ' sends the message that is fully drawn...
| Debug.Print "ThreadID:" & GetCurrentThreadId()
| Debug.Print "hWnd:" & FindWindow("ThunderDFrame", Me.Caption)
|
| hHook = SetWindowsHookEx(WH_CBT, _
| AddressOf
| modAPICommonDialogLib.WinProcCenterScreen, _
| GetWindowLong(FindWindow("ThunderDFrame",
| Me.Caption), GWL_HINSTANCE), _
| GetCurrentThreadId())
| <<< CODE <<<<
| here is the procedure pointed at by the 'AddressOf' function call:
|
| CODE
| Public Function WinProcCenterScreen(ByVal lMsg As Long, ByVal wParam As
| Long, ByVal lParam As Long, xForm As UserForm) As Long
| '================================================= ===='
| ' Developer : Philip Livingstone
| ' Workstation :
| '
| ' Purpose : Uses the API to centre a modal system dialog on the
| screen...
| ' Parameters : lMsg, passed in using the AddressOf function, basically
| we're
| ' subclassing the form...
| ' lParam, passed in using the AddressOf
function...
| ' Returns : WinProcCenterForm - Long
| '
| '================================================= ====
| Dim rectForm As RECT, rectMsg As RECT
| Dim X As Long, Y As Long
| If lMsg = HCBT_ACTIVATE Then
| ' Windows is about to activate the form that we're
subclassing...and
| we have
| ' applied a hook to it, to run this code when Windows sends the
| message...
| ' Show the MsgBox at a fixed location (0,0)
| GetWindowRect wParam, rectMsg
| X = rectMsg.left
| Y = rectMsg.top
| SetWindowPos wParam, 0, X, Y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or
| SWP_NOACTIVATE
| 'Release the CBT hook
| UnhookWindowsHookEx hHook
| End If
| WinProcCenterScreen = False
| End Function
| <<< END CODE<<<
|
| Can anyone help me find out what I am doing wrong - for example, should I
be
| including parameters in the WinProcCenterScreen AddressOf call?
|
| thanks
|
| Philip
|