Attribute VB_Name = "modClipboard"
Option Compare Database
Option Explicit
     
Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Declare PtrSafe Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As LongPtr) As LongPtr
Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr
Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As LongPtr
Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal dwBytes As LongPtr) As LongPtr
Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As Long
Declare PtrSafe Function GlobalSize Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr

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

    
    Function ClipBoard_GetData()
         
Dim lngHClipMemory As Long: Dim lptHClipMemory As LongPtr
Dim lptLpClipMemory As LongPtr
Dim strMyString As String
Dim lptRetVal As LongPtr

         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.
         lptHClipMemory = GetClipboardData(CF_TEXT)
         If IsNull(lngHClipMemory) Then
            MsgBox "Could not allocate memory"
            GoTo OutOfHere
         End If

         ' Lock Clipboard memory so we can reference
         ' the actual data string.
         lptLpClipMemory = GlobalLock(lptHClipMemory)

         If Not IsNull(lptLpClipMemory) Then

            strMyString = Space$(MAXSIZE)
            lptRetVal = lstrcpy(strMyString, lptLpClipMemory)
            lptRetVal = GlobalUnlock(lptHClipMemory)

            ' Peel off the null terminating character.
            strMyString = Mid(strMyString, 1, InStr(1, strMyString, Chr$(0), 0) - 1)
         Else
            MsgBox "Could not lock memory to copy string from."
         End If

OutOfHere:

         lptRetVal = CloseClipboard()
         ClipBoard_GetData = strMyString

      End Function
      
    Function ClipBoard_SetData(MyString As String)
            
Dim lngHGlobalMemory As Long: Dim lptLpGlobalMemory As LongPtr: Dim lptHGlobalMemory As LongPtr

Dim lptHClipMemory As LongPtr: Dim lngX As Long

         ' Allocate moveable global memory.
         '-------------------------------------------
         lptHGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)

         ' Lock the block to get a far pointer
         ' to this memory.
         lptLpGlobalMemory = GlobalLock(lptHGlobalMemory)

         ' Copy the string to this global memory.
         lptLpGlobalMemory = lstrcpy(lptLpGlobalMemory, MyString)

         ' Unlock the memory.

         If GlobalUnlock(lptHGlobalMemory) <> 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.
         lngX = EmptyClipboard()

         ' Copy the data to the Clipboard.

         lptHClipMemory = SetClipboardData(CF_TEXT, lptHGlobalMemory)

OutOfHere2:

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

         End Function

'***** Code start  ********
'code courtesy of
'Terry Kreft
'
Function ClipBoard_Clear()
  Call OpenClipboard(0&)
  Call EmptyClipboard
  Call CloseClipboard
End Function
'***** Code End ********


Public Sub TestClipBoard_SetData()
Dim strToAdd As String

    strToAdd = InputBox("Enter text to store on clipboard")
    ClipBoard_SetData strToAdd
End Sub
