The new vbAccelerator Site - more VB and .NET Code and Controls

Read a custom clipboard format

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,Clipboard,Interprocess Comms

Updated:

01/08/98

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


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

&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp &nbsp &nbsp CloseClipboard
&nbsp &nbsp 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
&nbsp &nbsp
&nbsp &nbsp
' If you don't have IE4 or greater, change the
&nbsp &nbsp ' format to "RTF Format" instead.
&nbsp &nbsp lID = GetClipboardIDForCustomFormat("HTML Format")
&nbsp &nbsp If (lID 0) Then
&nbsp &nbsp &nbsp &nbsp sText = GetClipboardDataAsString(Me.hWnd, lID)
&nbsp &nbsp &nbsp &nbsp Text1.Text = sText
&nbsp &nbsp End If

End Sub


&nbsp

Related Tips and Articles:

&nbsp

AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 01/08/98