ACC: How to Send Information to the Clipboard

ID: Q96900


The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0


SUMMARY

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

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

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual, Chapter 3, "Introducing Access Basic" in version 2.0.

NOTE: In version 2.0, you can use the OLE_COPY action to copy an OLE field to the clipboard. This article does not address this case.


MORE INFORMATION

Notes


  • 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.


  • You may have some Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive the duplicate procedure name error, remove or comment the Declare statement from your code.


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

   X = ClipBoard_SetData("This string will go to the Clipboard!") 


   X = ClipBoard_SetData("This string will go to the Clipboard!")

   '=================================================================
   ' General Declarations
   '=================================================================

   Option Compare Database   'Use database order for string comparisons
   Option Explicit

   Declare Function kb_OpenClipBoard% Lib "User"_
                    Alias "OpenClipBoard" (_
                    ByVal hwnd%)
   Declare Function kb_GlobalAlloc% Lib "Kernel" _
                    Alias "GlobalAlloc" (ByVal wFlags%, ByVal wBytes&)
   Declare Function kb_GlobalLock& Lib "Kernel" _
                    Alias "GlobalLock" (ByVal hMem%)
   Declare Function kb_lstrcpy& Lib "Kernel" _
                    Alias "lstrcpy" (ByVal lpString1 As Any, _
                    ByVal lpString2 As Any)
   Declare Function kb_GlobalUnLock% Lib "Kernel" _
                    Alias "GlobalUnLock" (ByVal hMem%)
   Declare Function kb_CloseClipBoard% Lib "User" Alias"CloseClipBoard"()
   Declare Function kb_EmptyClipBoard% Lib "USER" _
                    Alias "EmptyClipBoard" ()
   Declare Function kb_SetClipboardData% Lib "User" _
                    Alias "SetClipboardData" (ByVal wFormat%, _
                    ByVal hMem%)

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

   Function ClipBoard_SetData (MyString$)
      Dim hGlobalMemory%, lpGlobalMemory&, hClipMemory%, X%

   '-------------------------------------------
   ' Allocate movable global memory.
   '-------------------------------------------
   hGlobalMemory% = kb_GlobalAlloc(GHND, Len(MyString$) + 1)

   '-------------------------------------------
   ' Lock the block to get a far pointer
   ' to this memory.
   '-------------------------------------------
   lpGlobalMemory& = kb_GlobalLock(hGlobalMemory%)

   '-------------------------------------------
   ' Copy the string to this global memory.
   '-------------------------------------------
   lpGlobalMemory& = kb_lstrcpy(lpGlobalMemory&, MyString$)

   '-------------------------------------------
   ' Unlock the memory.
   '-------------------------------------------
   If kb_GlobalUnLock(hGlobalMemory%) <> 0 Then
      MsgBox "Could not unlock memory location. Copy aborted."
      GoTo OutOfHere2
   End If

   '-------------------------------------------
   ' Open the Clipboard to copy data to.
   '-------------------------------------------
   If kb_OpenClipBoard(0&) = 0 Then
      MsgBox "Could not open the Clipboard. Copy aborted."
      Exit Function
   End If

   '-------------------------------------------
   ' Clear the Clipboard.
   '-------------------------------------------
   X% = kb_EmptyClipBoard()

   '-------------------------------------------
   ' Copy the data to the Clipboard.
   '-------------------------------------------
   hClipMemory% = kb_SetClipboardData(CF_TEXT, hGlobalMemory%)

   OutOfHere2:

   If kb_CloseClipBoard() = 0 Then
      MsgBox "Could not close Clipboard."
   End If

   End Function 

Additional query words: api

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


Last Reviewed: August 5, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.