View Full Version : Clipboard stuff


ozinm
11-23-2005, 05:37 AM
Not sure who wrote the original code (if this yours PM me and I'll edit this message to give you full credit) but I've found this really useful.
there are 2 functions. One to get data from the clipboard and another to put data into it.
Just pop the code into a new module and away you go!

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
Declare Function GetClipboardData Lib "User32" (ByVal wFormat As _
Long) As Long
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) _
As Long


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


Function ClipBoard_GetData()
Dim hClipMemory As Long
Dim lpClipMemory As Long
Dim MyString As String
Dim RetVal As Long

If OpenClipboard(0&) = 0 Then
MsgBox "Cannot open Clipboard. Another app. may 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 we can reference
' the actual data string.
lpClipMemory = GlobalLock(hClipMemory)

If Not IsNull(lpClipMemory) Then
MyString = Space$(MAXSIZE)
RetVal = lstrcpy(MyString, lpClipMemory)
RetVal = 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:

RetVal = CloseClipboard()
ClipBoard_GetData = MyString

End Function


Function ClipBoard_SetData(MyString As String)
Dim hGlobalMemory As Long, lpGlobalMemory As Long
Dim hClipMemory As Long, X As Long

' Allocate movable 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

waltMA70
05-15-2006, 05:42 AM
Sorry about reviving this thread from the dead, but the code may be able to help my situation.

First off, I am very new to access and have very limited knowledge when it comes to coding - especially with SQL/access. My situatuion is as follows:

I intern in a very boring position where my boss gives me busy work to fil time. I have been filtering drawing files from a large database using access, printing out the query spreadsheets for reference, and manually searching through another database of .tif drawing files (corresponding to the first database) to select the filtered files for plotting. Before I export the drawings to the plotting program, they are transcribed into a new personal folder.

Could this code allow me to paste the filtered files into the 'personal folder', thereby sparing me the tedium of going therough the files to manually select the files to be transcribed?

If so, how can I begin to impliment the code to yield a solution?

Any help is much appreciated.

Pauldohert
05-18-2006, 12:49 AM
Can anyone tell me what the above code does?

Uncle Gizmo
12-22-2006, 11:39 AM
Can anyone tell me what the above code does?

http://www.access-programmers.co.uk/forums/showpost.php?p=550495&postcount=2