Option Explicit
Private Declare Function OpenClipboard Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function GetClipboardData Lib "user32" _
(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 Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Dest As Any, Src As Any, _
ByVal lSize As Long)
Private Declare Function lstrlenA Lib "kernel32" _
(ByVal lpString As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" _
(ByVal wFlags As Long, _
ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" _
(ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" _
(ByVal hMem As Long) As Long
Private Const GMEM_FIXED = &H0
Private Const XML_SPREADSHEET = 49365
Private Function PutDataInClipBoard(ByVal sData As String) As Boolean
Dim Buffer() As Byte
Dim hData As Long
Dim lpData As Long
If OpenClipboard(0&) Then
'Convert data to ANSI byte array.
Buffer = StrConv(sData & vbNullChar, vbFromUnicode)
'Allocate memory for buffer.
hData = GlobalAlloc(GMEM_FIXED, UBound(Buffer) + 1)
If hData Then
'Copy data to allocated memory.
lpData = GlobalLock(hData)
Call CopyMemory(ByVal lpData, Buffer(0), UBound(Buffer) + 1)
Call GlobalUnlock(hData)
'Put data into the clipboard
If SetClipboardData(XML_SPREADSHEET, hData) <> 0 Then
PutDataInClipBoard = True
End If
End If
CloseClipboard
End If
End Function
Private Function GetDataFromClipBoard() As String
Dim Buffer() As Byte
Dim lLen As Long
Dim hData As Long
Dim lpData As Long
'Open the clipboard.
If OpenClipboard(0&) <> 0 Then
'Get handle to the XML_Spreadsheet ClpBrd data.
hData = GetClipboardData(XML_SPREADSHEET)
'Lock the memory handle.
lpData = GlobalLock(hData)
lLen = lstrlenA(ByVal lpData)
If lLen Then
'Copy clpBrd data to Buffer.
ReDim Buffer(0 To (lLen - 1)) As Byte
CopyMemory Buffer(0), ByVal lpData, lLen
Call GlobalUnlock(hData)
GetDataFromClipBoard = StrConv(Buffer, vbUnicode)
End If
CloseClipboard
End If
End Function