OLE From a Developer's Perspective

The OLE specification includes a feature called OLE automation, which lets developers do three things:

Applications that can control another application's exposed objects are called OLE automation clients, and applications that expose objects are called OLE automation servers.

Although you need Visual Basic 4—or, for greater control and speed, C/C++—to create objects, you can use the objects that you create (or that are exposed by shrink-wrapped products) with any programming language that provides "client-support" for OLE automation. Exposed objects are considered to be objects only by a programming language that can manipulate them. For example, to a language that includes OLE automation capabilities, WordBasic is an object, the Microsoft Excel application itself is an object, and so are Microsoft Excel worksheets. People who use integrated business applications, however, may never know that another application is involved behind the scene. The following table shows the extent to which the Microsoft BASICs support OLE automation

How the Microsoft BASICs Implement OLE

Access 2

Microsoft Excel 5

Visual Basic 3

Visual Basic 4

Word 6

OLE Client (from an end-user's perspective)

X

X

X

OLE Server (from an end-user's perspective)

X

X

OLE automation client (can control objects exposed by other applications)

X

X

X

X

OLE automation server (exposes objects that can be controlled by another application)

X

X

X

Implement DDE

X

X

X

X

X


Where Am I Now?

One issue that comes up in the design of integrated applications is how to organize the application. There are three main organizational models:

Who's in Control?

Another issue that comes up when you design integrated applications is how much users should see of component applications. Here are a few rules of thumb, which depend on the nature of the integrated business application you're developing:

Minimizing Problems for Users

Whenever you build applications that open more than one Office product simultaneously, you can minimize the possibility that users get "out of memory" messages (or "hang") by testing to determine the percentage of free resources a particular inter-application operation needs to complete successfully. Once you've determined this, whenever users execute the command, check in code to see whether the application to be launched is already open. If it's not, make sure that this minimum is available.

When using OLE automation, you can trap the error generated by the GetObject function when the object (such as the Microsoft Excel application) isn't open. (See the section titled The CreatePIVOTTable Function presented later for an example.) If it isn't open, use the CheckResources function (shown below) in Access Basic, VBA for Microsoft Excel, or Visual Basic to verify whether there are sufficient free resources to open the product or a new instance of the product and continue processing (Chapter 6 of Developing Applications with Microsoft Office provides information on using Windows API functions such as GetFreeSystemResources. Remember that you may need to alias API functions).


Declare Function GetFreeSystemResources Lib "User" (ByVal     fuSysResource As Integer) As Integer

Function CheckResources ()
    Dim intFree As Integer
'Specify the go-ahead threshold
    Const THRESHOLD = 65

'The argument 0 specifies system resources; 1 specifies
'graphics resources (GDI); and 2 specifies User resources.
    intFree = GetFreeSystemResources(0)
    If intFree > THRESHOLD Then
        MsgBox "It's a GO!", 64, "Check Resources"
    Else
        MsgBox "NO GO. Resources only " & intFree & "% free.", 16, "Check Resources"
    End If

End Function

Although Word isn't an OLE automation client, you can control other applications through WordBasic and DDE. Use the FindWindow Windows API function to determine whether an Office product is open. Use following WordBasic code to check free system resources and then to launch the product or to warn users depending on the resources available.


Sub MAIN
'The argument 25 specifies the system ersources.
    FreeResources$ = GetSystemInfo$(25)
    MsgBox "Available disk space: " + FreeResources$ + " %."
End Sub