VB Books online..
The Object
Provides access to the system Clipboard.
Syntax
Clipboard
Remarks
The Clipboard object is used to manipulate text and graphics on the Clipboard. You can use this object to enable a user to copy, cut, and paste text or graphics in your application. Before copying any material to the Clipboard object, you should clear its contents by as performing a Clear method, such as Clipboard.Clear.
Note that the Clipboard object is shared by all Windows applications, and thus, the contents are subject to change whenever you switch to another application.
The Clipboard object can contain several pieces of data as long as each piece is in a different format. For example, you can use the SetData method to put a bitmap on the Clipboard with the vbCFDIB format, and then use the SetText method with the vbCFText format to put text on the Clipboard. You can then use the GetText method to retrieve the text or the GetData method to retrieve the graphic. Data on the Clipboard is lost when another set of data of the same format is placed on the Clipboard either through code or a menu command.
Methods
Clear Method
GetText Method
GetData Method
GetFormat Method
SetData Method
SetText Method
GetData Method
Returns a graphic from the Clipboard object. Doesn't support named arguments.
Syntax
object.GetData (format)
..
The GetData method syntax has these parts:
Part Description
object Required. An object expression that evaluates to an object in the Applies To list.
format Optional. A constant or value that specifies the Clipboard graphics format, as described in Settings. Parentheses must enclose the constant or value. If format is 0 or omitted, GetData automatically uses the appropriate format.
..
Settings
The settings for format are:
Constant Value Description
vbCFBitmap 2 Bitmap (.bmp files)
vbCFMetafile 3 metafile (.wmf files)
vbCFDIB 8 Device-independent bitmap (DIB)
vbCFPalette 9 Color palette
..
Remarks
These constants are listed in the Visual Basic (VB) object library in the Object Browser.
If no graphic on the Clipboard object matches the expected format, nothing is returned. If only a color palette is present on the Clipboard object, a minimum size (1 x 1) DIB is created.
Checking data Formats
You can use the GetFormat method to determine whether the data on the Clipboard is in a particular format. For example, you can disable the Paste command depending on whether the data on the Clipboard is compatible with the currently active control.
Code:
Private Sub mnuEdit_Click ()
' Click event for the Edit menu.
mnuCut.Enabled = True
mnuCopy.Enabled = True
mnuPaste.Enabled = False
If TypeOf Screen.ActiveControl Is TextBox Then
If Clipboard.GetFormat(vbCFText) Then mnuPaste.Enabled = True
ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
If Clipboard.GetFormat(vbCFText) Then mnuPaste.Enabled = True
ElseIf TypeOf Screen.ActiveControl Is ListBox Then
If Clipboard.GetFormat(vbCFText) Then mnuPaste.Enabled = True
ElseIf TypeOf Screen.ActiveControl Is PictureBox _
Then
If Clipboard.GetFormat(vbCFBitmap) Then mnuPaste.Enabled = True
Else
' Can't cut or copy from the other types
' of controls.
mnuCut.Enabled = False
mnuCopy.Enabled = False
End If
End Sub
Note You might also want to check for other data formats with the constants vbCFPalette, vbCFDIB, and vbCFMetafile. If you want to replace a picture’s palette using Clipboard operations, you should request vbCFBitmap rather than vbCFDIB from the Clipboard. See "Working with 256 Colors” later in this chapter for more information on working with the color palette.
Getdata method
This example uses the GetData method to copy a bitmap from the Clipboard object to a form. To try this example, paste the code into the Declarations section of a form, and then press F5 and click the form.
Code:
Private Sub Form_Click ()
Const CF_BITMAP = 2 ' Define bitmap format.
Dim Msg ' Declare variable.
On Error Resume Next ' Set up error handling.
Msg = "Choose OK to load a bitmap onto the Clipboard."
MsgBox Msg ' Display message.
Clipboard.Clear ' Clear Clipboard.
Clipboard.SetData LoadPicture("PAPER.BMP") ' Get bitmap.
If Err Then
Msg = "Can't find the .bmp file."
MsgBox Msg ' Display error message.
Exit Sub
End If
Msg = "A bitmap is now on the Clipboard. Choose OK to copy "
Msg = Msg & "the bitmap from the Clipboard to the form "
MsgBox Msg ' Display message.
Picture = Clipboard.GetData() ' Copy from Clipboard.
Msg = "Choose OK to clear the form."
MsgBox Msg ' Display message.
Picture = LoadPicture() ' Clear form.
End Sub
SetData Method
Puts a picture on the Clipboard object using the specified graphic format. Doesn't support named arguments.
Syntax
object.SetData data, format
..
The SetData method syntax has these parts:
Part Description
object Required. An object expression that evaluates to an object in the Applies To list.
data Required. A graphic to be placed on the Clipboard object.
format Optional. A constant or value that specifies one of the Clipboard object formats recognized by Visual Basic, as described in Settings. If format is omitted, SetData automatically determines the graphic format.
..
Settings
The settings for format are:
Constant Value Description
vbCFBitmap 2 Bitmap (.bmp files)
vbCFMetafile 3 Metafile (.wmf files)
vbCFDIB 8 Device-independent bitmap (DIB)
vbCFPalette 9 Color palette
..
Remarks
These constants are listed in the Visual Basic (VB) object library in the Object Browser.
You set the graphic that is to be placed onto the Clipboard object with either the LoadPicture function or the Picture property of a Form, Image, or PictureBox.
Working with the ActiveControl Property
If you want the Copy, Cut, and Paste commands to work with any text box that has the focus, use the ActiveControl property of the Screen object. The following code provides a reference to whichever control has the focus:
Screen.ActiveControl
You can use this fragment just like any other reference to a control. If you know that the control is a text box, you can refer to any of the properties supported for text boxes, including Text, SelText, and SelLength. The following code assumes that the active control is a text box, and uses the SelText property:
Code:
Private Sub mnuCopy_Click ()
Clipboard.Clear
Clipboard.SetText Screen.ActiveControl.SelText
End Sub
Private Sub mnuCut_Click ()
Clipboard.Clear
Clipboard.SetText Screen.ActiveControl.SelText
Screen.ActiveControl.SelText = ""
End Sub