ActiveX and OLE Overview

Description

Object Linking and Embedding (OLE) is about using documents generated by one application inside another application. ActiveX is a Microsoft rebranding of OLE with more focus on smart Windows controls. Automation is a simple but powerful way to link software together without having to share source code. It lets a client program "drive" a server program. Automation is also used for dynamically loadable "in-process" libraries.

Objects are registered at install time in the Windows registry. Entries describe:

  • Properties, event, and methods (functions)

  • Enumerated types (constants)

  • How to create, load, and use the object

 Placing an ActiveX Control on a Form

  1. Select the following icon and sketch the control on your form.

  2. Select the ActiveX control you want to use and click Insert.

  3. Right click on the control and select Event > ActiveXEvent to see where to place code to respond to the control's events.

  4. To directly manipulate properties of the control, your code should be structured as follows.

dim object as P
object = my_control_name.activex.this
if object.title = "" then
    object.title = "Hello World"
    object.refresh()
end if

This example uses the Calendar Control 8.0 ActiveX control. This example has a form based on a table called "customer". This table has a field called date_of_birth. If you wanted the ActiveX control to show this field value, then you would put the following code in the form's OnFetch() event:

dim t as P
dim dt as D
dim p as P
t = table.current()
dt = t.date_of_birth
'get a pointer to the ActiveX control
p = activex1.activex.this
'set the ActiveX control's .value property to the date
p.value = ctodt("" + dt)

 Using Xdialog with ActiveX

In the Xdialog environment, you must declare a variables to identify the control. In the script below imaging.editctrl.1 is the "friendly" name of the control. The last dot and digit defines the optional version number.

dim img as P
dim img.object as P
dim img.class as C
img.class="imaging.editctrl.1"
ui_dlg_box("Demo",<<%dlg%
{activex=80,25img}
%dlg%,<<%code%
dim obj as P
obj = img.object
if obj.title = "" then
    obj.title = "Hello World"
    obj.refresh()
end if
%code%)

The developer must take the responsibility to see that the ActiveX control is on his client's computer.

Use regsvr32 controlname to register an ActiveX control on a computer.

 Three Ways to Create an Object

There are three ways to create an object.

dim MyObject as ole::adodb.connection
dim MyObject as P
MyObject = ole.create("adodb.connection")
dim MyObject as P
MyObject = ole.getobject("c:\summary.xls")

In the last case the object is the document itself. After using OLE.CREATE()to create a new object, access the object using dot (".") syntax. For example:

dim someobject as P
someobject = ole.create("owc.spreadsheet")
someobject.range("A1").select()
someobject.activecell = "Name"

 Practical Examples

The following script is a practical example of OLE.

objNetwork = ole.create("Wscript.Network")
strComputer = objnetwork.ComputerName
ui_msg_box("Computer Name", strComputer)

This script reports on the users in a domain.

dim colAccounts as P
dim msg as C
dim userarray[1] as C
colAccounts = ole.getobject("WinNT://Alphasoftware,domain")
userarray[1] = "user"
colAccounts.filter = userarray
for each objUser in colAccounts
    msg = msg + objUser.Name + crlf()
next
ui_msg_box("Users", msg)

This script spell checks text using Microsoft Word.

dim wb as P
dim oldtext as C
dim newtext as C
wb = ole.create("Word.Basic")
wb.filenew()
wb.insert("Ths iz pur splling fur sore!")
wb.toolsspelling()
wb.editselectall()
oldtext = web.selection()
wb.fileexit(2)
ui_msg_box("Corrected text", oldtext)

This script retrieves data from an Access database.

dim Output as C
objConn = ole.create("ADODB.Connection")
objRs = ole.create("ADODB.Recordset")
objComm = ole.create("ADODB.Command")
connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/northwind.mdb;Persist Security Info=False;"
CommandText = "SLECT CompanyName FROM Customers ORDER BY CompanyName"
objConn.Open(ConnectionSTring)
objRs.open(CommandText, objConn)
while .not. objRs.eof
    output = output + objRs.fields.item("CompanyName").value + crlf()
    objRs.MoveNext()
end while
objRs.close()
objConn.close()

See Also