Microsoft KB Archive/94162

= ACC: How to Retrieve Information from the Clipboard (1.x/2.0) =

Article ID: 94162

Article Last Modified on 5/9/2003

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q94162



SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

There is no command in Microsoft Access to retrieve information from the Clipboard. To retrieve information from the Clipboard, you need to define an Access Basic function that calls several Windows API functions. This article defines a function that retrieves text from the Clipboard.



MORE INFORMATION
Add the code defined below to an Access Basic module. Make sure that the declare functions are all listed on one line and that the MsgBox procedure is also defined on one line. Because you may have some of the Windows API functions defined in an underlying library, some of them might not be needed. If this is the case, remove or comment the declarations from your code.

To call the function, you can make a string assignment to the returned value, such as:

  ReturnString$ = ClipBoard_GetData

Sample Code
NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore when re-creating this code in Access Basic.

Option Explicit

Declare Function OpenClipBoard% Lib &quot;User&quot; (ByVal hwnd%) Declare Function GetClipboardData% Lib &quot;User&quot; (ByVal wFormat%) Declare Function GlobalAlloc% Lib &quot;Kernel&quot; (_     ByVal wFlags%, ByVal dwBytes&) Declare Function GlobalLock& Lib &quot;Kernel&quot; (ByVal hMem%) Declare Function lstrcpy& Lib &quot;Kernel&quot; (_     ByVal lpString1 As Any, ByVal lpString2 As Any) Declare Function GlobalUnlock% Lib &quot;Kernel&quot; (ByVal hMem%) Declare Function CloseClipBoard% Lib &quot;User&quot; Declare Function GlobalSize& Lib &quot;Kernel&quot; (ByVal hMem%)

Global Const GHND = &H42 Global Const CF_TEXT = 1 Global Const MAXSIZE = 4096

Function ClipBoard_GetData Dim hClipMemory% Dim lpClipMemory& Dim MyString$ Dim Junk& Dim X%

If OpenClipBoard(0&) = 0 Then MsgBox &quot;Could not open the Clipboard. Another _ application could have it open&quot; Exit Function End If

'---     ' Obtain the handle to the global memory ' block that is referencing the text. '---     hClipMemory% = GetClipboardData(CF_TEXT) If IsNull(hClipMemory%) Then MsgBox &quot;Could not allocate memory&quot; GoTo OutOfHere End If

'---     ' Lock Clipboard memory so you can reference ' the actual data string. '---     lpClipMemory& = GlobalLock(hClipMemory)

If Not IsNull(lpClipMemory&) Then MyString$ = Space$(MAXSIZE) Junk& = lstrcpy(MyString$, lpClipMemory) X% = GlobalUnlock(hClipMemory)

'peel off the null terminating character MyString$ = Mid(MyString$, 1, InStr(1, MyString$, Chr$(0), 0) - 1) Else MsgBox &quot;Could not lock memory to copy string from.&quot; End If

OutOfHere:

X% = CloseClipBoard ClipBoard_GetData = MyString$

End Function

