Read A Custom Clipboard Format

VB limits you to getting information in just the standard clipboard formats. However, a number of applications paste information in other formats, for example Rich Text Format, HTML format and so forth. This tip shows you how to read the data for a custom format from the clipboard as a string.

Start a new project in VB. Add a new module, then add the following code to it:

' Clipboard functions: 
Private Declare Function OpenClipboard Lib "USER32" _
    (ByVal hWnd As Long) As Long 
Private Declare Function CloseClipboard Lib "USER32" () As Long 
Private Declare Function GetClipboardData Lib "USER32" _
    (ByVal wFormat As Long) As Long 
Private Declare Function IsClipboardFormatAvailable Lib "USER32" _
    (ByVal wFormat As Long) As Long 
Private Declare Function RegisterClipboardFormat Lib "USER32" _
    Alias "RegisterClipboardFormatA" _
    (ByVal lpString As String) As Long 
' Memory functions: 
Private Declare Function GlobalLock Lib "kernel32" _
    (ByVal hMem As Long) As Long 
Private Declare Function GlobalUnlock Lib "kernel32" _
    (ByVal hMem As Long) As Long 
Private Declare Function GlobalSize Lib "kernel32" _
    (ByVal hMem As Long) As Long 
Private Declare Sub CopyMemory Lib "kernel32" _
    Alias "RtlMoveMemory" ( _
    lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long) 

Public Function GetClipboardIDForCustomFormat( _
        ByVal sName As String _
    ) As Long 
Dim wFormat As Long 
    wFormat = RegisterClipboardFormat(sName & Chr$(0)) 
    If (wFormat > &HC000&) Then 
        GetClipboardIDForCustomFormat = wFormat 
    End If 
End Function 
Public Function GetClipboardDataAsString( _
        ByVal hWndOwner As Long, 
        ByVal lFormatID As Long _
    ) As String 
Dim bData() As Byte 
Dim hMem As Long 
Dim lSize As Long 
Dim lPtr As Long 
    
    ' Open the clipboard for access: 
    If (OpenClipboard(hWndOwner)) Then 
        ' Check if this data format is available: 
        If (IsClipboardFormatAvailable(lFormatID) <> 0) Then 
            ' Get the memory handle to the data: 
            hMem = GetClipboardData(lFormatID) 
            If (hMem <> 0) Then 
                ' Get the size of this memory block: 
                lSize = GlobalSize(hMem) 
                If (lSize > 0) Then 
                    ' Get a pointer to the memory: 
                    lPtr = GlobalLock(hMem) 
                    If (lPtr <> 0) Then 
                        ' Resize the byte array to hold the data: 
                        ReDim bData(0 To lSize - 1) As Byte 
                        ' Copy from the pointer into the array: 
                        CopyMemory bData(0), ByVal lPtr, lSize 
                        ' Unlock the memory block: 
                        GlobalUnlock hMem 
                        
                        ' Now return the data as a string: 
                        GetClipboardDataAsString = StrConv(bData, vbUnicode) 

                    End If 
                End If 
            End If 
        End If 
        CloseClipboard 
    End If 

End Function 

To test out the function, add a Command button and a TextBox to the project's form. Set the TextBox's multi-line property to True, and set its ScrollBar property to 2 (Vertical).

The following code will get any information pasted onto the clipboard in HTML Format. HTML Format is pasted by Internet Explorer v4 and above, and gives you an entire HTML document based on the selection you have copied from the clipboard.

Private Sub Command1_Click() 
Dim lID As Long 
Dim sText As String 
    
    ' If you don't have IE4 or greater, change the 
    ' format to "RTF Format" instead. 
    lID = GetClipboardIDForCustomFormat("HTML Format") 
    If (lID <> 0) Then 
        sText = GetClipboardDataAsString(Me.hWnd, lID) 
        Text1.Text = sText 
    End If 

End Sub