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:
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