OLE Objects and Pictures

Puts pictures, files and other OLE objects into the text of a RichTextBox, counts them and finds them.

Dim Path As String
Path = RtbTinyBitmapFile()
RichTextBox1.Text = "ab"
RichTextBox1.SelStart = 1
RichTextBox1.OLEObjectsAddFromPicture LoadPicture(Path)   ' between "a" and "b"
Kill Path
Debug.Print RichTextBox1.OLEObjectsCount; RichTextBox1.TextLength
RichTextBox1.SelStart = 1
RichTextBox1.SelLength = 1
Debug.Print RichTextBox1.GetSelType = RtfSelTypeObject
' Output:
'  1  3
' True

RtbTinyBitmapFile in this sample is a function of the program’s own that writes a small bitmap file under %TEMP% and returns its path.

Objects in the text

Each object takes the place of one character of the text, at which the box draws it. Text gives a space for it, and TextRTF holds the object itself. The methods that insert an object put it in place of the selection, or at the insertion point:

OLEObjectsCount returns the number of objects, and OLEObjectsGet one of them. A program that works on the objects through the interfaces of OLE gets the interface of the control from GetOLEInterface.

When an object leaves the text, because the user or the program deleted it, the control raises OLEDeleteObject.

Methods

GetOLEInterface

Returns the IRichEditOle interface of the rich edit control, through which a program that declares that interface reaches the objects of the text.

Syntax: object.GetOLEInterface As IUnknown

OLEObjectsAdd

Inserts an OLE object that the program has made, in place of the selection or at the insertion point.

Syntax: object.OLEObjectsAdd LpOleObject

LpOleObject
required A LongPtr: the address of the object’s IOleObject interface, as ObjPtr returns it.

OLEObjectsAddFromFile

Inserts a file as an OLE object, in place of the selection or at the insertion point. Windows makes the object, through the program that the file’s type belongs to.

Syntax: object.OLEObjectsAddFromFile FileName [, LinkToFile ]

FileName
required A String: the full path of the file.
LinkToFile
optional A Boolean: True to insert a link to the file, which shows what the file holds when it changes. The default, False, puts a copy of the file into the text.

A file that does not exist raises run-time error 53, File not found.

OLEObjectsAddFromPicture

Inserts a picture, in place of the selection or at the insertion point. The picture is an object that shows it and cannot be edited.

Syntax: object.OLEObjectsAddFromPicture Picture [, ClipFormat ]

Picture
required A picture object that holds a bitmap, a metafile or an enhanced metafile. Nothing, or a picture that holds nothing, inserts nothing.
ClipFormat
optional A number: the format of the picture, vbCFBitmap, vbCFMetafile or vbCFEMetafile, members of ClipboardConstants, which the method checks against the picture.

A picture of another kind, such as an icon, raises run-time error 380. A ClipFormat that is not the format of the picture raises run-time error 461, Specified format doesn’t match format of data.

OLEObjectsCount

Returns the number of OLE objects in the text.

Syntax: object.OLEObjectsCount As Long

OLEObjectsGet

Returns the address of the IOleObject interface of one object of the text, or 0. The address holds no reference of its own: it is good while the object is in the text.

Syntax: object.OLEObjectsGet( IndexObj [, CharPos ] ) As LongPtr

IndexObj
required A Long: the number of the object, counted from 0 in the order of the text; or -1 for the selected object; or -2 for the object at CharPos.
CharPos
optional A Long: the place of the object in the text, counted from 0, when IndexObj is -2.

Events

OLEDeleteObject

Raised when an OLE object is about to leave the text. The object need not be destroyed: an object that the user cuts goes to the clipboard.

Syntax: object_OLEDeleteObject( ByVal LpOleObject As LongPtr )

LpOleObject
The address of the object’s IOleObject interface.

See Also