EnumClipboardFormats returns immidiatelly zero while clipboard data exists (Excel 2013)

Ibrahim M

New Member
Joined
Dec 9, 2016
Messages
2
Hi,

I am using EnumClipboardFormats function to go through the clipboard formats.
First I copy some data from the cells from newly created Excel spreadsheet. I made a spreadsheet with only EnumClipboardFormats function in it. In this spreadsheet I see various clipboard formats. I have the other spreadsheet (with much more macro code in it) and this spreadsheet returns zero when EnumClipboardFormats is called immediately.

When I call GetLastError I get zero value returned.

Microsoft documentation says the following:
"If the function succeeds, the return value is the clipboard format that follows the specified format, namely the next available clipboard format. If the function fails, the return value is zero. To get extended error information, call GetLastError. If the clipboard is not open, the function fails. If there are no more clipboard formats to enumerate, the return value is zero. In this case, the GetLastError function returns the value ERROR_SUCCESS. This lets you distinguish between function failure and the end of enumeration."

Since GetLastError returns zero I assume that this means that clipboard is empty, which is not true. Excel version where this goes wrong is Excel 2013. The same spreadsheet where this goes wrong in Excel 2013, gives the good results in Excel version 2010.

Can somebody help me here? Any help would be greatly appreciated.

The code that I am using is the following:

Code:
Sub FetchClipboardText()
    Dim i As Long, CF_Format  As Long
    Dim Handle As LongPtr, Ptr As LongPtr
            
    'Enumerate the clipboard formats

If OpenClipboard(0) Then
  CF_Format = EnumClipboardFormats(0&)
  Do While CF_Format <> 0
    S = String(255, vbNullChar)
    i = GetClipboardFormatName(CF_Format, S, 255)
    S = Left(S, i)
    If CF_Format = xlClipboardFormatVALU Then
      Handle = GetClipboardData(CF_Format)
      Ptr = GlobalLock(Handle)
      Application.CutCopyMode = False
      ClipboardContent = Space$(lstrlen(ByVal Ptr))
      lstrcpy ClipboardContent, ByVal Ptr
      GlobalUnlock Ptr
      Exit Do
    End If
    CF_Format = EnumClipboardFormats(CF_Format)
  Loop
  CloseClipboard
End If
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Solved. In the Workbook_Activate method was a line of code which emptied the clipboard.
Code:
Application.CellDragAndDrop = True
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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