Determine every format available on the clipboard, including custom formats

Whilst VB allows you to determine if the standard, recognised clipboard formats are on the clipboard, you can't determine if any other formats are present. So, for example, if you want to know if the clipboard contains data from Excel (which always pastes at least its own customised format called 'Wk1'), or if you want to determine whether Rich Text format is available to enable a Paste menu, you can't do it.

This tip shows how to determine all the formats currently available on the clipboard, including the customised formats.

Start a new project in VB. Add a Command button and a ListBox to the project's form, then add the following code:

Private Declare Function CountClipboardFormats Lib "USER32" () As Long 
Private Declare Function EnumClipboardFormats Lib "USER32" ( _
    ByVal wFormat As Long) As Long 
Private Declare Function OpenClipboard Lib "USER32" ( _
    ByVal hWnd As Long) As Long 
Private Declare Function CloseClipboard Lib "USER32" () As Long 
Private Declare Function GetClipboardFormatName Lib "USER32" _
    Alias "GetClipboardFormatNameA" _
    (ByVal wFormat As Long, ByVal lpString As String, _
    ByVal nMaxCount As Long) As Long 
Private Enum EPredefinedClipboardFormatConstants 
    [_First] = 1 
    CF_TEXT = 1 
    CF_BITMAP = 2 
    CF_METAFILEPICT = 3 
    CF_SYLK = 4 
    CF_DIF = 5 
    CF_TIFF = 6 
    CF_OEMTEXT = 7 
    CF_DIB = 8 
    CF_PALETTE = 9 
    CF_PENDATA = 10 
    CF_RIFF = 11 
    CF_WAVE = 12 
    CF_UNICODETEXT = 13 
    CF_ENHMETAFILE = 14 
    CF_HDROP = 15 
    CF_LOCALE = 16 
    CF_MAX = 17 
    [_Last] = 17 
End Enum 


Private Property Get FormatName( _ 
        ByVal lFormatId As Long _ 
    ) As String 
' Returns the format name for a clipboard format id: 
Dim lSize As Long 
Dim sBuf As String 
Dim lR As Long 
    
    If (lFormatId >= EPredefinedClipboardFormatConstants.[_First] _
        And lFormatId <= EPredefinedClipboardFormatConstants.[_Last]) Then 
        ' For pre-defined formats, we have to make the text 
        ' up ourselves: 
        Select Case lFormatId 
        Case CF_TEXT 
            FormatName = "Text" 
        Case CF_BITMAP 
            FormatName = "Bitmap Picture" 
        Case CF_METAFILEPICT 
            FormatName = "Meta-File Picture" 
        Case CF_SYLK 
            FormatName = "Microsoft Symbolic Link (SYLK) data." 
        Case CF_DIF 
            FormatName = "Software Arts' Data Interchange information." 
        Case CF_TIFF = 6 
            FormatName = "Tagged Image File Format (TIFF) Picture" 
        Case CF_OEMTEXT 
            FormatName = "Text (OEM)" 
        Case CF_DIB 
            FormatName = "DIB Bitmap Picture" 
        Case CF_PALETTE 
            FormatName = "Colour Palette" 
        Case CF_PENDATA 
            FormatName = "Pen Data" 
        Case CF_RIFF 
            FormatName = "RIFF Audio data" 
        Case CF_WAVE 
            FormatName = "Wave File" 
        Case CF_UNICODETEXT 
            FormatName = "Text (Unicode)" 
        Case CF_ENHMETAFILE 
            FormatName = "Enhanced Meta-File Picture" 
        Case CF_HDROP 
            FormatName = "File List" 
        Case CF_LOCALE 
            FormatName = "Text Locale Identifier" 
        End Select 
    Else 
        ' For custom formats, we can ask the Clipboard for 
        ' the registered name: 
        lSize = 255 
        sBuf = String$(lSize, 0) 
        lR = GetClipboardFormatName(lFormatId, sBuf, lSize) 
        If (lR <> 0) Then 
            FormatName = Left$(sBuf, lR) 
        End If 
    End If 
End Property 

Private Sub Command1_Click() 
Dim lR As Long 
Dim iCount As Long 
    
    List1.Clear 

    If (OpenClipboard(Me.hWnd)) Then 
        lR = EnumClipboardFormats(0) 
        If (lR <> 0) Then 
            Do 
                iCount = iCount + 1 
                List1.AddItem FormatName(lR) 
                List1.ItemData(List1.NewIndex) = lR 
                lR = EnumClipboardFormats(lR) 
            Loop While lR <> 0 
        End If 
    End If 
    CloseClipboard 
    
End Sub 

When you click the command button, the list box will be filled with a list of available data formats. The List field is set to the format name, and the item data of each item is set to the clipboard format's ID. To see custom formats in the list, try copying something from Word, Write or Internet Explorer before clicking the button.