The public Sub com_msforms_class_dataobject_test () ' Msforms.dataobject object, which can be used to communicate the data of each module in a VBA project, can store text in various formats, and the format name must be an integer or String: ' 1.dataobject.gettext ([format]) from the DataObject object, gets the stored text of the formatted text, such as omitting the format parameter, which will get the "format 1 text, which is the default stored format of the object The format parameter can also be an integer other than 1, or an arbitrary string, ' string not case-insensitive ' 2.dataobject.settext (Text,[format]) to store the text in a certain format on the DataObject object. If the format parameter is omitted, the text is stored as the default text in format 1, the format parameter can be an integer other than 1, or any string, and the string does not distinguish between large ' lowercase ' 3.dataobject.getformat (format) for checking DataObject a list of formats, check if the corresponding format format exists ' 4.dataobject.getfromclipboard () take the text from the system Clipboard once and save it as a DataObject format of 1 text if ' The system Clipboard is now empty, then call the Dataobject.getfromclipboard () method, will be the format of the DataObject object in the form 1 "The text and format are deleted ' 5.dataobject.putinclipboard () Put the text of the DataObject object Format 1 into the system Clipboard, after which the DataObject object of the grid "style 1 of the text storage unit with the system Clipboard sharing, accesses than either change, and another accesses than either, from the functional design point of view, this may be a bug," the dataob of its opposite function Ject. GetFromClipboard () does not share the location where the data is stored, so to speak. ' 6.dataobject.clear () empties the DataObject stored format list and the text data, and the call to this method cannot be broken because the tune ' uses the Dataobject.putinclipboard () method, The DataObject object Format 1 text storage unit and the systemData sharing between the system shear plates; To completely disconnect, only the variable is reinitialized to a new DataObject object ' 7.dataobject.startdrag ([effect]) starting with the data drag-and-drop based on the MSForms interface program. Is the most complex method of this object, you can call this method from the MouseMove event of any one of the msforms controls, and after the call it will block the MouseMove event handler, you can give "any control that you want to accept drag-and-drop data, add BeforeDragOver and BeforeDropOrPaste two event handlers, in the former set the "Cancel=true" to facilitate the subsequent event processing passed over the event parameter data (DataObject type) in the information (this data is "' Front MouseMove event tune Before using the Dataobject.startdrag () method to manually set the DataObject object, that is, before calling ' ', you need to first set the text to be passed with Dataobject.settext (Text,[format]). Otherwise pass an empty DataObject ' ' object, also have no meaning?!? Dim Clip as New Msforms.dataobject, Clip1 as New Msforms.dataobject "1. Format 1 is the default value for the formatting parameter of the SetText () and the GetText () method, but it is not set before this A format is not present in the ' Format list ' of the DataObject object Debug.Print 1, Clip.getformat (1) ' False ' 2.DataObject object all methods of the format parameter, if it is a string, Then this string is case insensitive to Clip.settext "Smith", "Name" Debug.Print 2, Clip.gettext ("name") Debug.Print 2, Clip.gettext ("name ") ' 3.SetText (Text,[format]) method, the parameter format defaults to 1 Clip.settext" Hello World "Debug.Print 3, Clip.getformat (1)Clip.settext "Oh, Yeah", 2 Debug.Print 3, Clip.getformat (2) "4.GetText ([format]) method, parameter format defaults to 1 Debug.Print 4, Clip.gettext ' Hello World Debug.Print 4, Clip.gettext (2) ' Oh, Yeah ' 5.SetText () and GetText () method The format parameter must be an integer or a string Clip.settext "hao123", "website" Debug.Print 5, Clip.gettext ("website") clip.settext "format is", Debug.Print 5, Clip.gettext (32) ' 5.1 format if not an integer number, the CInt conversion is performed, but there are exceptions, so it is recommended not to use this kinky trick clip.settext "format for 3.2", 3.2 Debug.Print 5.1, Clip . GetText (3.2) Clip.settext "format true text", True ' format is-1 "' Debug.Print Clip.gettext (-1) ' ERROR clip.settext ' format for FAL The SE text ", False ' format is 0 Debug.Print 5.1, Clip.gettext (False) ' 6.DataObject object all methods of the format parameter, can be a string, but cannot be an empty string or vbnullstr ing ' clip.settext ' hello ', ' ' clip.settext ' good Good Study ", vbNullString ' 7. You can also use Dataobject.putinclipboard () to Place the text in the DataObject object in the system Clipboard Note: The text imported into the system Clipboard is the text data formatted as 1 in the DataObject object Clip.putinclipboard ' paste found is Hello World ' 8. Can be done by DATAOBJECT.GETFROMCLipboard () to import the text from the system Clipboard into a field in the Format 1 text in the DataObject object Clip.settext "12345" Clip.getfromclipboard "8.1" below the GetText () call , "Hello World", which was put into the system clipboard after the last call to the PutInClipboard () method, was imported, "but imported is the new set of" 12345 ", so PutInClipboard () Method will bind the DataObject object to the Clipboard, which is certainly a functional bug ' more specific experiment see below Debug.Print 8.1, Clip.gettext ' here is the output of "Hello World" Clip.sett Ext "Hello World" Debug.Print 8.1, Clip.gettext ' 9.Clear () method will empty all format list with text data Clip.clear Debug.Print 9, clip.ge Tformat (1) before the 9.1Clear () method is called, if the DataObject object is shared with the system Clipboard data, the data in the system Clipboard will be cleared "================" Paste Try "================" ' 9.2 If the Clipboard text is empty, the GetFromClipboard () method will delete the DataObject object in the format 1, and the text will not be accessible clip.getfromclipboard ' Debug.Print 9.2, CLIP.G Ettext ' 9.3 The above code clears the data in the DataObject, but it is still not possible to disconnect the data sharing between the DataObject and the system Clipboard created above Clip.settext 12345 Debu G.print 9.3, Clip.gettext ' if the Clear () is broken, this should be "Hello World", because "12345" has not been Putin ' 9.4 to disconnect the data between the DataObject and the system Clipboard, preferably The method is to reinitialize the object Set Clip = New msforms.dataobject Clip.settext "Hello World" Clip1.getfromclipboard ' Note: Here is Clip1 Debug.Print 9.4, Clip1.gettext ' NOTE: Here is Clip1, if not Disconnect, you should output the "Hello World" 9.5GetFromClipboard () method does not share the DataObject object with the system Clipboard data Clip1.settext "Hello World" Note: this is CLIP1 Clip.getfromclipboard ' NOTE: Here is the clip Debug.Print 9.5, Clip.gettext ' NOTE: Here is the clip, if a share is established, the "Hello World" End Sub should be output
VBA's Msforms.dataobject Object