Option Explicit #If VBA7 Then Private Declare PtrSafe Function GlobalLock _ Lib "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr Private Declare PtrSafe Function GlobalUnlock Lib _ "kernel32.dll" (ByVal hMem As LongPtr) As LongPtr Private Declare PtrSafe Function GlobalAlloc _ Lib "kernel32.dll" (ByVal wFlags As LongPtr, _ ByVal dwBytes As LongPtr) As LongPtr Private Declare PtrSafe Function OpenClipboard _ Lib "user32.dll" (ByVal hWnd As LongPtr) As LongPtr Private Declare PtrSafe Function EmptyClipboard _ Lib "user32.dll" () As LongPtr Private Declare PtrSafe Function GetClipboardData _ Lib "user32.dll" (ByVal wFormat As Long) As Long Private Declare PtrSafe Function SetClipboardData _ Lib "user32.dll" (ByVal wFormat As LongPtr, _ ByVal hMem As LongPtr) As LongPtr Private Declare PtrSafe Function CloseClipboard _ Lib "user32.dll" () As LongPtr Private Declare PtrSafe Function IsClipboardFormatAvailable _ Lib "user32.dll" (ByVal wFormat As Long) As Long Private Declare PtrSafe Function lstrcpy _ Lib "kernel32.dll" (ByVal lpString1 As Any, _ ByVal lpString2 As Any) As LongPtr Private Declare PtrSafe Function GlobalSize _ Lib "kernel32.dll" (ByVal hMem As Long) As Long #Else Private Declare Function GlobalLock _ Lib "kernel32.dll" (ByVal hMem As Long) As Long Private Declare Function GlobalUnlock _ Lib "kernel32.dll" (ByVal hMem As Long) As Long Private Declare Function GlobalAlloc _ Lib "kernel32.dll" (ByVal wFlags As Long, _ ByVal dwBytes As Long) As Long Private Declare Function GlobalSize _ Lib "kernel32.dll" (ByVal hMem As Long) As Long Private Declare Function OpenClipboard _ Lib "user32.dll" (ByVal hWnd As Long) As Long Private Declare Function EmptyClipboard _ Lib "user32.dll" () As Long Private Declare Function GetClipboardData _ Lib "user32.dll" (ByVal wFormat As Long) As Long Private Declare Function SetClipboardData _ Lib "user32.dll" (ByVal wFormat As Long, _ ByVal hMem As Long) As Long Private Declare Function CloseClipboard _ Lib "user32.dll" () As Long Private Declare Function IsClipboardFormatAvailable _ Lib "user32.dll" (ByVal wFormat As Long) As Long Private Declare Function lstrcpy _ Lib "kernel32.dll" _ Alias "lstrcpyW" (ByVal lpString1 As Long, _ ByVal lpString2 As Long) As Long #End If Const GHND = &H42 Const CF_TEXT = 1 Const MAXSIZE = 4096 Function ClipBoardCut(ClipText As String) #If VBA7 Then Debug.Print "VBA7" #Else Debug.Print "VBA else" #End If #If Win64 Then Debug.Print "64Bit" #Else Debug.Print "32Bit" #End If #If Win64 Then Dim hGlobalMemory As LongPtr Dim lpGlobalMemory As LongPtr Dim hClipMemory As LongPtr Dim X As Long #Else Dim hGlobalMemory As Long Dim lpGlobalMemory As Long Dim hClipMemory As Long Dim X As Long #End If Debug.Print Len(ClipText) hGlobalMemory = GlobalAlloc(GHND, Len(ClipText) + 1) lpGlobalMemory = GlobalLock(hGlobalMemory) lpGlobalMemory = lstrcpy(lpGlobalMemory, ClipText) If GlobalUnlock(hGlobalMemory) <> 0 Then MsgBox "Could not unlock memory location. Copy aborted." GoTo OutOfHere2 End If If OpenClipboard(0&) = 0 Then MsgBox "Could not open the Clipboard. Copy aborted." Exit Function End If EmptyClipboard hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory) OutOfHere2: If CloseClipboard() = 0 Then MsgBox "Could not close Clipboard." End If End Function Function ClipBoardPaste() As String Const CF_UNICODETEXT As Long = 13& #If Win64 Then Dim iStrPtr As LongPtr Dim iLen As LongPtr Dim iLock As LongPtr #Else Dim iStrPtr As Long Dim iLen As Long Dim iLock As Long #End If Dim ClipText As String OpenClipboard 0& If IsClipboardFormatAvailable(CF_UNICODETEXT) Then iStrPtr = GetClipboardData(CF_UNICODETEXT) Debug.Print iStrPtr If iStrPtr Then iLock = GlobalLock(iStrPtr) Debug.Print iLock iLen = GlobalSize(iStrPtr) Debug.Print iLen ClipText = String$(iLen \ 2& - 1&, vbNullChar) Debug.Print ClipText lstrcpy StrPtr(ClipText), iLock Debug.Print ClipText GlobalUnlock iStrPtr End If ClipBoardPaste = ClipText Debug.Print Len(ClipText) End If CloseClipboard End Function