Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Images on Clipboard (Cut, Paste, etc) and VBA?

Status
Not open for further replies.

xhonzi

Programmer
Jul 29, 2003
196
US
Okay,
I've seen this done, but not how.

I have a table and the unique key is an unitelligible string. The jpegs that relate to each record are named after the unique key. Up till now, to name a picture file, I've browsed to the record, looked at the id (unique key) and then saved the jpeg accordingly. What I would like to do is copy the image to the clipboard and then right click on the form (displaying the picture) and then Paste. The paste would just tell the code to save the clipboard contents to the specific file. Anyway, I could do all of this except I don't know how to use VBA to access the clipboard. Any help?

Thanks,
Xhonzi
 
Try this. Copy this code into a separate module for clarity sake:

Code:
Option Compare Database
Option Explicit

      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

      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 ExitHere
         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)

ExitHere:

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

         End Function

Then you can call it like this from another module:

Code:
Call ClipBoard_SetData( ... object to copy to clipboard ... )

Let me know how it works out for you.
 
Wow... You've got to be kidding me!

But, if you're not, Thanks!

Xhonzi
 
Okay, quick questions:
This looks like it's meant for text, not images... right?

And it also looks like copying to the clipboard... not from it... right?

Xhonzi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top