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

ID: Q94162


The information in this article applies to:


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 "User" (ByVal hwnd%)
   Declare Function GetClipboardData% Lib "User" (ByVal wFormat%)
   Declare Function GlobalAlloc% Lib "Kernel" (_
      ByVal wFlags%, ByVal dwBytes&)
   Declare Function GlobalLock& Lib "Kernel" (ByVal hMem%)
   Declare Function lstrcpy& Lib "Kernel" (_
      ByVal lpString1 As Any, ByVal lpString2 As Any)
   Declare Function GlobalUnlock% Lib "Kernel" (ByVal hMem%)
   Declare Function CloseClipBoard% Lib "User" ()
   Declare Function GlobalSize& Lib "Kernel" (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 "Could not open the Clipboard. Another _
            application could have it open"
         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 "Could not allocate memory"
         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 "Could not lock memory to copy string from."
      End If

   OutOfHere:

      X% = CloseClipBoard()
      ClipBoard_GetData = MyString$

   End Function 


REFERENCES

For an example of how to retrieve information from the Clipboard in Microsoft Access 95 and 97, please see the following article in the Microsoft Knowledge Base:

Q138910 How to Retrieve Information from the Clipboard (95/97)


Keywords          : kbprg 
Version           : 1.0 1.1 2.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: March 12, 1999