Accessing Clipboard from VB
The Clipboard is a Virtual Area where an application can place some data which can be accessed by it later or by some other application. While complete control over clipboard requires using Windows API functions, Visual Basic provides a basic set of clipboard functions as methods to the Clipboard object.
The data that is copied to the Clipboard, or pasted from can be of several types are are predefined as contstants as under :
Const vbCFText = 1 ' Plain Text Const vbCFBitmap = 2 ' Bitmap Const vbCFMetafile = 3 ' Metafile Picture Format Const vbCFDIB = 8 ' Device Independent Bitmap FOrmat Const vbCFPalette = 9 ' Color Palette Const vbCFEMetafile = 14 ' Enhanced Metafile Format Const vbCFFiles = 15 ' Files Const vbCFLink = -16640 (&HFFFFBF00) ' Link Const vbCFRTF = -16639 (&HFFFFBF01) ' Rich Text Format
More than one clipboard object can be placed on the keyboard, each in a different format, while representing the same information.
The four basic funtions that you will require most are
Clipboard.Clear() - Clears the current Clipboard contents Clipboard.SetText(Str As String, [Format]) - Copies data to the clipboard (default being text, unless stated specifically using optional Format parameter) Clipboard.GetFormat(Format as Integer) Returns True or False depending on whether Clipboard contained data in that partcular format Clipboard.GetText([Format]) Returns the data, Text if optional Format parameter not passed, from the Clipboard.
Similar commands to Set or Get picture data to / from Clipboard are :
Clipboard.SetData(Picture as IPictureDisp,[Format]) Clipboard.GetData([Format]) as IPictureDisp
The data that is passed / recieved above are IPictureDisp, which is the Interface to Picture Data.
Let’s make a sample application to get clear with the copy functions.
Create a new project place a Image control imgImage, multiline TextBox txtText and a command button cmdButton. Set the picture property of the Image to show image on the form. Add some string to the Textbox.

Add code as below to the Click event of the button
Private Sub cmdButton_Click()
Clipboard.Clear
Clipboard.SetText txtText
Clipboard.SetData imgImage, vbCFBitmap
End Sub
The code above will clear the clipboard on press of the button. It will then place to the clipboard the text from the textBox and the image as Bitmap Format.
Now open the Wordpad and paste from the clipboard, and you will get the text pasted there. Click on “Paste Special…” from the menu and a dialog box will appear with three options one for the text, other for bitmap and yet another for Device Independant Bitmap. Selecting any of the last two will result in pasting of the image.
And a point to note here… the bitmap we pasted was of type Bitmap. But you also received option for device independant Bitmap. It is so for the reason that Windows Clipboard provides facility for conversion between related formats.



Спасибо!!! На Вашем сайте часто появляются очень интересные посты! Очень поднимаете мое настроение.
gavapiomegree
May 24, 2009 at 5:51 pm
Hello!
Very Interesting post! Thank you for such interesting resource!
PS: Sorry for my bad english, I’v just started to learn this language
See you!
Your, Raiul Baztepo
RaiulBaztepo
March 29, 2009 at 12:21 am
Thank you very much. It was very useful for me.
Interested
September 18, 2008 at 3:41 pm
This does not work for VB console applications in VB 2008. Clipboard is not an object aparently.
Dave
April 15, 2008 at 6:51 pm
Hi Jalaj,
this one is really informative aricle.
Can you please tell me how to copy and paste multiple controls, e.g. i want to copy image/picture & caption of label(below the picture) from a VB form and i want to paste it on a word document.
Please hele me brother.
regards,
kapil
kapil
January 24, 2008 at 5:35 am
@Toha –
Thanks for pointing out inability to handle unicode text. I will update here as earliest as possible (after finding it, of course!)
Jalaj
December 27, 2007 at 4:17 am
Thanks a lot, brother. I was searching google for VB Clipboard method and found this article very useful.
However, can you please you tell me how can I copy unicode text (Such as Bengali) to clipboard?
Toha
December 26, 2007 at 12:47 pm
Can you tell me how to copy a file to the clipboard, for pasting onto an email later. I’ve tried ‘Clipboard.SetData mdbPath & Format(Date, “yyyymmdd”) & “.fsl”, vbCFFiles’ but it tells me ‘Object required. Thanks in advance.
Jonathan
May 15, 2007 at 6:08 pm
Thanks for the feedback. Following it, I have darkened the text font color, and the font size in code which went small after moving to Custom-CSS has been rectified to make it fairly readable.
Jalaj
April 29, 2007 at 10:37 pm
The information on this webpage is quite useful but it is exceptionally hard to make use it because:
(a) poor choice of font/background colors
(b) small type size [way too small!]
tony millar
April 29, 2007 at 6:46 am