United States*
Microsoft.com Home|Site Map
Microsoft*
Search Microsoft.com for:
Help and Support 
|Select a Product|Search (KB)

ACC2000: How to Send Information to the Clipboard

Article ID:210216
Last Review:June 24, 2004
Revision:1.0
This article was previously published under Q210216
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

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 Microsoft Windows API functions. This article shows you how to create a function that copies text to the Clipboard.

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.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
1.Start Microsoft Access and open any database or project.
2.Create a module and type or paste 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
					
3.Type or paste 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

					
4.To test this function, type the following line in the Immediate window, and then press ENTER.
? ClipBoard_SetData("To Clipboard")
Press CTRL+V (the shortcut for Paste) and note that "To Clipboard" is pasted into the Immediate window from the Clipboard.

REFERENCES

For more information about declaring Windows API functions, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type declare statement in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

APPLIES TO
Microsoft Access 2000 Standard Edition
Keywords: 
kbhowto kbprogramming KB210216

Article Translations

 

Related Support Centers

Microsoft Access 2000

Other Support Options

Contact Microsoft
Phone Numbers, Support Options and Pricing, Online Help, and more.
Customer Service
For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
Newsgroups
Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.

©2005 Microsoft Corporation. All rights reserved. Terms of Use |Trademarks |Privacy Statement