Tips Forum Logo
Tips Forum Home PageSpacerVisit Microsoft Knowledge BaseMAUG Home PageMAUG Russian Home Page

Function to set Windows Clipboard text
Function to set Windows Clipboard text

Applies to: Access 32 bit (95,97, 2000, XP & 2003)


Just make a new module and paste text below there.

Option Explicit
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function CloseClipboard Lib "User32" () As Long
Private Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "User32" () As Long
Private Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Private Declare Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As Long) As Long

Private Const GHND = &H42
Private Const CF_TEXT = 1
Private Const MAXSIZE = 4096
Public Function fSetClipBoardTest(txtString As String) As Boolean
Dim hGlobalMemory As Long, lpGlobalMemory As Long
Dim hClipMemory As Long

' Allocate movable global memory.
'-------------------------------------------
hGlobalMemory = GlobalAlloc(GHND, Len(txtString) + 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, txtString)

' Unlock the memory.
If GlobalUnlock(hGlobalMemory) <> 0 Then
MsgBox "Could not unlock memory location. Copy aborted."
fSetClipBoardTest = False
GoTo fSetClipBoardTest_Exit
End If

' Open the Clipboard to copy data to.
If OpenClipboard(0&) = 0 Then
MsgBox "Could not open the Clipboard. Copy aborted."
fSetClipBoardTest = False
GoTo fSetClipBoardTest_Exit
End If

' Clear the Clipboard.
Call EmptyClipboard

' Copy the data to the Clipboard.
hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)

If CloseClipboard() = 0 Then
MsgBox "Could not close Clipboard."
End If
fSetClipBoardTest = True

fSetClipBoardTest_Exit:

End Function

From  Alex Dybenko , Entry Date 06.07.2005



Microsoft Access Solutions

Written and designed by
Alex Dybenko
Send us your comments
Last Update: 2007-10-15
Copyright 1998-2007 by MAUG

Alex & Access