Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using AddressOf in VBA

Status
Not open for further replies.

pcawdron

Programmer
Jun 14, 2000
109
AU
Anyone have any idea how to use AddressOf in a VBA Module?

I'm trying to use some API calls to block the mouse right-click, but I keep getting an error that doesn't occur with the same code in VB6.

Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

Public Const GWL_WNDPROC = -4
Public Const WM_RBUTTONUP = &H205
Public lpPrevWndProc As Long
Private lngHWnd As Long

Public Sub Hook(hWnd As Long)
lngHWnd = hWnd
lpPrevWndProc = SetWindowLong (lngHWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Public Sub UnHook()
Dim lngReturnValue As Long
lngReturnValue = SetWindowLong(lngHWnd, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hw As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Select Case uMsg
Case WM_RBUTTONUP
MsgBox "Right Click Intercepted"
Case Else
WindowProc = CallWindowProc(lpPrevWndProc, hw, _
uMsg, wParam, lParam)
End Select

End Function


Runs perfectly in VB6 but not inVBA, although VBA DOES Recognise AddressOf because it activates a drop down menu containing relevant Modules & Proceedures.

To try this in VB 6, throw the code above into a VB6 Module. Place a textbox on a blank form, Text1, and add Call Hook (Text1.hwd) [ color black] to the Form Load event and Call UnHook to the Form Unload event. BEWARE : If you don't add the UnHook you'll cause a General Protection Fault

Help me ObiWan...

Thanks,
Peter
 
the "handle" parameter (lngHWnd) in VB is not the same as Access
try putting the me! in from of it
like so

lpPrevWndProc = SetWindowLong (Me!lngHWnd, GWL_WNDPROC, AddressOf WindowProc)

DougP, MCP

Visit my WEB site to see how Bar-codes can help you be more productive
 
Thanks for the suggestion. I tried...

lpPrevWndProc = SetWindowLong (Me!lngHWnd, GWL_WNDPROC, AddressOf WindowProc)

And...

lpPrevWndProc = SetWindowLong (lngHWnd, GWL_WNDPROC, AddressOf Me!WindowProc)

And...

lpPrevWndProc = SetWindowLong (lngHWnd, GWL_WNDPROC, AddressOf API.WindowProc ) 'API is the name of this Module

But nothing works. In all cases Access VBA spits out the same error and highlights the word AddressOf. Its definately the AddressOf that's causing the hiccup, but I don't know why.

The hwnd I'm passing is OK, it's for a form rather than a single control as that's all Access will allow.

There's no problem with the API call, the problems with establishing the AddressOf the function WindowProc.

I have no idea why this fails...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top