Windows Clipboard Read - Access application hanging

P

pwu

Hi,

I'm trying to read the clipboard. As each new text item is put into
the clipboard by Access form vba code is called by use of the Winproc
callback.

I have one push button which starts the capture:
Private Sub btnCapture_Click()
MsgBox "Capturing...."
HookForm Me
End Sub
I have a push button which ends the capture and prints out everything
that was copied into the clipboard.
Private Sub btnCaptureEnd_Click()
MsgBox "End of Capture."
UnHookForm Me
MsgBox EntireLine
End Sub

However the problem is that once the capture start button is pressed
the application hangs.
That is I cannot hit the capture end button
Can someone please explain how to fix this problem?

The code is shown below.

Thanks for any help
=======================================================================
+
+
+ C O D
E
+
+
+
=======================================================================
Option Compare Database

Private Const WM_DRAWCLIPBOARD = &H308
Private Const WM_CHANGECBCHAIN = &H30D
Private Const WM_DESTROY = &H2
Private Const GWL_WNDPROC = -4

Private Const GMEM_MOVABLE = &H2&
Private Const GMEM_DDESHARE = &H2000&
Private Const CF_TEXT = 1

Private Const CLIPBOARDFORMATNOTAVAILABLE = 1
Private Const CANNOTOPENCLIPBOARD = 2
Private Const CANNOTGETCLIPBOARDDATA = 3
Private Const CANNOTGLOBALLOCK = 4
Private Const CANNOTCLOSECLIPBOARD = 5
Private Const CANNOTGLOBALALLOC = 6
Private Const CANNOTEMPTYCLIPBOARD = 7
Private Const CANNOTSETCLIPBOARDDATA = 8
Private Const CANNOTGLOBALFREE = 9

Dim PrevProc As Long
Dim NextClip As Long
Dim EntireLine As String

'---------------------------------------------------------------
' Reads the data in the clipboard
'----------------------------------------------------------------
Public Function GetClipBrdText() As Variant
Dim hMemory As Long
Dim lpMemory As Long
Dim strText As String
Dim lngSize As Long
Dim varRet As Variant

varRet = ""

' Is there text on the clipboard? If not, error out.
If Not CBool(IsClipboardFormatAvailable _
(CF_TEXT)) Then
varRet = CVErr(CLIPBOARDFORMATNOTAVAILABLE)
GoTo GetClipBrdTextDone
End If

' Open the clipboard
If Not CBool(OpenClipboard(0&)) Then
varRet = CVErr(CANNOTOPENCLIPBOARD)
GoTo GetClipBrdTextDone
End If

' Get the handle to the clipboard data
hMemory = GetClipboardData(CF_TEXT)
If Not CBool(hMemory) Then
varRet = CVErr(CANNOTGETCLIPBOARDDATA)
GoTo GetClipBrdTextCloseClipboard
End If

' Find out how big it is and allocate enough space
' in a string
lngSize = GlobalSize(hMemory)
strText = Space$(lngSize)

' Lock the handle so we can use it
lpMemory = GlobalLock(hMemory)
If Not CBool(lpMemory) Then
varRet = CVErr(CANNOTGLOBALLOCK)
GoTo GetClipBrdTextCloseClipboard
End If

' Move the information from the clipboard memory
' into our string
Call MoveMemory(strText, lpMemory, lngSize)

' Truncate it at the first Null character because
' the value reported by lngSize is erroneously large
strText = Left$(strText, InStr(1, strText, Chr$(0)) - 1)
' MsgBox "This item is " & GetClipboardSequenceNumber()
' Free the lock
Call GlobalUnlock(hMemory)

GetClipBrdTextCloseClipboard:
' Close the clipboard
If Not CBool(CloseClipboard()) Then
varRet = CVErr(CANNOTCLOSECLIPBOARD)
End If

GetClipBrdTextDone:
If Not IsError(varRet) Then
GetClipBrdText = strText
Else
GetClipBrdText = varRet
End If
End Function

'-------------------------------------------------------------------------------------------------------------------------------------------------------
' Routine called when a Windows event is called
'--------------------------------------------------------------------------------------------------------------------------------------------------------
Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long,
ByVal wParam As Long, ByVal lParam As Long) As Long
Dim buf As Variant
WindowProc = 0
If uMsg = WM_DRAWCLIPBOARD Then
' Got some text
buf = GetClipBrdText()
EntireLine = EntireLine & " " & buf
End If
WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam)
End Function

'----------------------------------------------------------------------------------------------------------------------
' Registers routine for windows event and clipboard viewer
'-----------------------------------------------------------------------------------------------------------------------
Public Sub HookForm(F As Form)
Dim Msg As String
PrevProc = SetWindowLong(F.hwnd, GWL_WNDPROC, AddressOf
WindowProc)
If PrevProc = 0 Then
MsgBox "Windows event handler registration failed"
Exit Sub
End If
'Register this form as a Clipboardviewer
NextClip = SetClipboardViewer(F.hwnd)
If NextClip = Null Then
MsgBox "Clipboard registration failed"
End If
End Sub
'---------------------------------------------------------------------------------------------
' Deregisters routine for windows event and clipboard viewer
'----------------------------------------------------------------------------------------------
Public Sub UnHookForm(F As Form)
If NextClip <> Null Then
ChangeClipboardChain F.hwnd, NextClip
End If
If PrevProc <> 0 Then
SetWindowLong F.hwnd, GWL_WNDPROC, PrevProc
End If
End Sub
 

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