Skip to content

Instantly share code, notes, and snippets.

@darkdreamingdan
Created May 10, 2016 18:29
Show Gist options
  • Save darkdreamingdan/aca55d3c7c2eeb8d74fed87aa99160ef to your computer and use it in GitHub Desktop.
Save darkdreamingdan/aca55d3c7c2eeb8d74fed87aa99160ef to your computer and use it in GitHub Desktop.
Grabbing Clipboard Data using VBA in Excel
Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function _
CloseClipboard& Lib "user32" ()
Private Declare PtrSafe Function _
OpenClipboard& Lib "user32" (ByVal hWnd&)
Private Declare PtrSafe Function _
EmptyClipboard& Lib "user32" ()
Private Declare PtrSafe Function _
GetClipboardData& Lib "user32" (ByVal wFormat&)
Private Declare PtrSafe Function _
GlobalSize& Lib "kernel32" (ByVal hMem&)
Private Declare PtrSafe Function _
GlobalLock& Lib "kernel32" (ByVal hMem&)
Private Declare PtrSafe Function _
GlobalUnlock& Lib "kernel32" (ByVal hMem&)
Private Declare PtrSafe Sub CopyMem Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length&)
#Else
Private Declare Function _
CloseClipboard& Lib "user32" ()
Private Declare Function _
OpenClipboard& Lib "user32" (ByVal hWnd&)
Private Declare Function _
EmptyClipboard& Lib "user32" ()
Private Declare Function _
GetClipboardData& Lib "user32" (ByVal wFormat&)
Private Declare Function _
GlobalSize& Lib "kernel32" (ByVal hMem&)
Private Declare Function _
GlobalLock& Lib "kernel32" (ByVal hMem&)
Private Declare Function _
GlobalUnlock& Lib "kernel32" (ByVal hMem&)
Private Declare Sub CopyMem Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length&)
#End If
Private Function GetData(ByVal Format&, abData() As Byte) As Boolean
Dim hWnd&, Size&, Ptr&
If OpenClipboard(0&) Then
' Get memory handle to the data
hWnd = GetClipboardData(Format)
' Get size of this memory block
If hWnd Then Size = GlobalSize(hWnd)
' Get pointer to the locked memory
If Size Then Ptr = GlobalLock(hWnd)
If Ptr Then
' Resize the byte array to hold the data
ReDim abData(0 To Size - 1) As Byte
' Copy from the pointer into the array
CopyMem abData(0), ByVal Ptr, Size
' Unlock the memory
Call GlobalUnlock(hWnd)
GetData = True
End If
EmptyClipboard
CloseClipboard
DoEvents
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment