Visual Basic Reference

Visual Studio 6.0

SetData Method (ActiveX Controls)

See Also   Example   Applies To

Inserts data into a DataObject object using the specified data format.


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 Optional A variant containing the data to be passed to the DataObject object.
format Optional. A constant or value that specifies the format of the data being passed, as described in Settings.


The settings for format are:

Constant Value Description
vbCFText 1 Text (.txt files)
vbCFBitmap 2 Bitmap (.bmp files)
vbCFMetafile 3 Metafile (.wmf files)
vbCFEMetafile 14 Enhanced metafile (.emf files)
vbCFDIB 8 Device-independent bitmap (DIB)
vbCFPalette 9 Color palette
vbCFFiles 15 List of files
vbCFRTF -16639 Rich text format (.rtf files)


These constants are listed in the Visual Basic (VB) object library in the Object Browser.

The data argument is optional. This allows you to set several different formats that the source component can support without having to load the data separately for each format. Multiple formats are set by calling SetData several times, each time using a different format. If you wish to start fresh, use the Clear method to clear all data and format information from the DataObject.

The format argument is also optional, but either the data or format argument must be specified. If data is specified, but not format, then Visual Basic will try to determine the format of the data. If it is unsuccessful, then an error is generated. When the target requests the data, and a format was specified, but no data was provided, the sources OLESetData event occurs, and the source can then provide the requested data type.

It's possible for the GetData and SetData methods to use data formats other than those listed in Settings, including user-defined formats registered with Windows via the RegisterClipboardFormat() API function. However, there are a few caveats:

  • The SetData method requires the data to be in the form of a byte array when it does not recognize the data format specified.

  • The GetData method always returns data in a byte array when it is in a format that it doesn't recognize, although Visual Basic can transparently convert this returned byte array into other data types, such as strings.

  • The byte array returned by GetData will be larger than the actual data when running on some operating systems, with arbitrary bytes at the end of the array. The reason for this is that Visual Basic does not know the data's format, and knows only the amount of memory that the operating system has allocated for the data. This allocation of memory is often larger than is actually required for the data. Therefore, there may be extraneous bytes near the end of the allocated memory segment. As a result, you must use appropriate functions to interpret the returned data in a meaningful way (such as truncating a string at a particular length with the Left function if the data is in a text format).