ACC: How to Send Information to the Clipboard (95/97)

Last reviewed: August 28, 1997
Article ID: Q138909
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

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

Microsoft Access does not have a command that sends information to the Clipboard. To post information to the Clipboard, you need to define a Visual Basic for Applications function that calls several Windows API functions. This article defines a function that copies text to the Clipboard.

(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 that case.)

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

MORE INFORMATION

To copy information to the Clipboard, follow these steps.

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

  1. Create a module and type the following lines in the Declarations section:

          Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
    
             As Long
          Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
             As Long
          Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _
             ByVal dwBytes As Long) As Long
          Declare Function CloseClipboard Lib "User32" () As Long
          Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _
             As Long
          Declare Function EmptyClipboard Lib "User32" () As Long
          Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
             ByVal lpString2 As Any) As Long
          Declare Function SetClipboardData Lib "User32" (ByVal wFormat _
             As Long, ByVal hMem As Long) As Long
    
          Public Const GHND = &H42
          Public Const CF_TEXT = 1
          Public Const MAXSIZE = 4096
    
    

  2. Type the following procedure:

          Function ClipBoard_SetData(MyString As String)
    
             Dim hGlobalMemory As Long, lpGlobalMemory As Long
             Dim hClipMemory As Long, X As Long
    
             ' Allocate moveable global memory.
             '-------------------------------------------
             hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)
    
             ' Lock the block to get a far pointer
             ' to this memory.
             lpGlobalMemory = GlobalLock(hGlobalMemory)
    
             ' Copy the string to this global memory.
             lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)
    
             ' Unlock the memory.
             If GlobalUnlock(hGlobalMemory) <> 0 Then
                MsgBox "Could not unlock memory location. Copy aborted."
                GoTo OutOfHere2
             End If
    
             ' Open the Clipboard to copy data to.
             If OpenClipboard(0&) = 0 Then
                MsgBox "Could not open the Clipboard. Copy aborted."
                Exit Function
             End If
    
             ' Clear the Clipboard.
             X = EmptyClipboard()
    
             ' Copy the data to the Clipboard.
             hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
    
          OutOfHere2:
    
             If CloseClipboard() = 0 Then
                MsgBox "Could not close Clipboard."
             End If
    
             End Function
    
    

  3. To test this function, type the following line in the Debug window, and then press ENTER.

          ? ClipBoard_SetData("To Clipboard")
    

    Type CTRL+V (the shortcut for Paste) and note that "To Clipboard" is pasted into the Debug Window from the Clipboard.

REFERENCES

For more information about declaring Windows API functions, search for "Declare Statement" and then "Declare Statement" using the Microsoft Access 97 Help Index.

Keywords          : kbprg PgmApi
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.