How do I correctly use GetClipboardData to retrieve what is currently saved in the Clipboard?

Lexus1

New Member
Joined
Aug 2, 2018
Messages
7
Dear all,

I have the following code:

Code:
[COLOR=#000000][FONT=verdana]Public Declare Function GetClipboardData Lib "user32" (CF_UNICODETEXT) As Long[/FONT][/COLOR]
[COLOR=#000000][FONT=verdana]Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long[/FONT][/COLOR]
[COLOR=#000000][FONT=verdana]Public Declare Function EmptyClipboard Lib "user32" () As Long[/FONT][/COLOR]
[COLOR=#000000][FONT=verdana]Public Declare Function CloseClipboard Lib "user32" () As Long[/FONT][/COLOR]
[COLOR=#000000][FONT=verdana][/FONT][/COLOR]
[FONT=verdana][COLOR=#000000]Sub test1()[/COLOR][/FONT]
[FONT=verdana][/FONT]
[FONT=verdana][COLOR=#000000]Dim wat As String[/COLOR][/FONT][FONT=verdana][/FONT]
[FONT=verdana][COLOR=#000000]OpenClipboard (0&)[/COLOR][/FONT]
[FONT=verdana][COLOR=#000000]wat = GetClipboardData(wFormat)[/COLOR][/FONT]
[FONT=verdana][COLOR=#000000]CloseClipboard[/COLOR][/FONT]
[FONT=verdana][/FONT]
[FONT=verdana][COLOR=#000000] [FONT=verdana][COLOR=#000000]MsgBox wat[/COLOR][/FONT]  [/COLOR][/FONT]
[FONT=verdana][/FONT]
[FONT=verdana][COLOR=#000000]End Sub[/COLOR][/FONT]

[FONT=verdana][COLOR=#000000]Sub test2()[/COLOR][/FONT]
[FONT=verdana][COLOR=#000000]OpenClipboard (0&)[/COLOR][/FONT]
[FONT=verdana][COLOR=#000000]EmptyClipboard[/COLOR][/FONT]
[FONT=verdana][COLOR=#000000]CloseClipboard[/COLOR][/FONT][FONT=verdana][/FONT]
[FONT=verdana][COLOR=#000000]End Sub[/COLOR][/FONT]
While test2 works just fine (clears the clipboard), I can't get test1 to work. It (msgbox) always returns the value of 0. Could you please tell me what I would have to do to fix it. In essence I would expect it to retrieve / display the text that I have copied last.

Thank you for any help!
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You need more than that. This function will return the copied text:

Code:
Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _
   As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function GetClipboardData Lib "User32" (ByVal wFormat As _
   Long) As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal _
   dwBytes As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
   As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
   As Long
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) _
   As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
   ByVal lpString2 As Any) As Long
 
Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096
Function ClipBoard_GetData()
   Dim hClipMemory As Long
   Dim lpClipMemory As Long
   Dim MyString As String
   Dim RetVal As Long
 
   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.
   hClipMemory = GetClipboardData(CF_TEXT)
   If IsNull(hClipMemory) Then
      MsgBox "Could not allocate memory"
      GoTo OutOfHere
   End If
 
   ' Lock Clipboard memory so we can reference
   ' the actual data string.
   lpClipMemory = GlobalLock(hClipMemory)
 
   If Not IsNull(lpClipMemory) Then
      MyString = Space$(MAXSIZE)
      RetVal = lstrcpy(MyString, lpClipMemory)
      RetVal = GlobalUnlock(hClipMemory)
       
      ' Peel off the null terminating character.
      MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
   Else
      MsgBox "Could not lock memory to copy string from."
   End If
 
OutOfHere:
 
   RetVal = CloseClipboard()
   ClipBoard_GetData = MyString
 
End Function
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top