
One of the most powerful features of Windows 98 is Object Linking and Embedding (OLE). With OLE you can move information from one application to another quickly and easily. An example of OLE is dynamically linking information from an Excel spreadsheet to a Word document. This can be a tremendous time saver.
An in-depth knowledge of the technology behind OLE isn't necessary for one to begin taking full advantage of Windows 98's powerful data exchange capabilities. With only a basic knowledge of OLE, a user can transfer data
from application to application transparently. Of course, understanding OLE concepts in detail will help you use your Windows 98 applications more efficiently.
This chapter introduces the main components of OLE 2.0, DDE, and file association, covering the following topics:
OLE enables you to create data (called an object) in an application. An object can be almost anything, such as a bitmap, an audio file, a video clip, or a spreadsheet. You can then embed or link that object data to other data created with another application. You don't have to exit the original application to make necessary refinements even when editing the object within your original application. Editing becomes much more convenient because you can then edit from within the compound document.
An OLE object is a finite unit of information created by using an OLE- compatible application. Bitmaps, text, line art, charts, and spreadsheets can all be considered objects. Objects can be divided into two components: presentation data and native data. Presentation data is that which allows the object to appear onscreen and to communicate with a printer. Native data is the information needed to edit or maintain the object (such as toolbars and controls).
Scrap objects (see Figure 16.1) are special objects that Windows 98 enables you to create. They are sections of text from the word processor that you can place directly on the desktop. The desktop then functions as a visible Clipboard.
FIGURE 16.1 A scrap object.
The procedure for the creation of a scrap object is as follows:
FIGURE 16.2 The Scrap Object Properties dialog box.
You can use OLE container applications to create and manage compound documents. Compound documents are containers that hold objects created using another application. Productivity is greatly enhanced by this OLE feature. The term compound document encompasses a wide variety of possibilities. For example, a compound document can be any container of objects, such as a spreadsheet that contains a word processing document or a presentation that contains a sales forecast from a spreadsheet. Another example could be a Word document that contains a sound file. These containers are documents that can seamlessly incorporate objects of different formats. Sound clips, spreadsheets, text, and bitmaps are all examples of objects commonly used in compound documents. Each object is created and maintained by its object application, although Object Linking and Embedding allows for the integration of services of different objects.
When you are using a compound document, your computer acts with the functionality of a single application for each object application.
You should familiarize yourself with the terms presented in Table 16.1 to get the most out of this chapter.
TABLE 16.1 OLE Terms and Definitions
| Term | Meaning |
| Objects | Information created by OLE-compatible applications |
| Native data | All of an object's information needed to display, control, and edit the data |
| Presentation data | That portion of an object's information necessary to display the object |
| Compound document | An OLE document made up of objects created with more than one application and linked with OLE |
| Drag and drop | Moving objects from one application or document to another using the mouse (or keyboard) to grab the object and drag it to the new location |
| Container | The application that is used to hold the various objects that make up a compound document |
| DDE | Dynamic Data Exchange |
| Visual editing | (Also called in-place editing) The capability to edit an object embedded in another application without having to switch between applications |
| Scrap objects | Special OLE objects that can be placed directly on the Windows 98 desktop |
Windows 98 uses OLE 2.0. Windows 95 and Windows 3.1 also used OLE 2.0, but all previous versions of Windows used OLE 1.0. The material in this chapter refers to OLE 2.0.
The technology behind OLE 2.0 has enabled software vendors to use the object concept to facilitate the creation of sophisticated applications. These new applications provide a new level of application interoperability. What this means is that different applications can work in cooperation with each other in better harmony, even when the applications are developed by different software venders. This new level of application integration in OLE is achieved by the creating and defining of a set of standard interfaces through which one application accesses the services of another. This standard is based on the Component Object Model, which specifies how data objects interact. The Component Object Model defines a standard for object implementation independent of programming language. This provides the basis for OLE 2.0 functionality. Regardless of which particular software developer's product is used to create a component object, the software conforms to the Component Object Model by implementing and using the interfaces that support object interaction.
OLE objects are stored and maintained in a container application. (The application doesn't have to be associated with the object application.) The container document is the part of the container application that holds the embedded object.
NOTE: Previous versions of OLE referred to container applications as "client applications."
The communication between the container application and the object application takes place through interfaces available through the OLE library file's process intercommunication. An object application acts as a server that provides the data object. (In previous versions of OLE, these applications were sometimes referred to as server applications.) Object applications also can use OLE interfaces to communicate with container applications.
You can use OLE to link or embed an object into a document. Linking uses less storage space. Linking is the process by which you place only the object's presentation data and a pointer to its native data in a document. The native data for this object does not exist in the container but is in some other location, such as a file on the disk. There is a limitation of linking versus embedding. This is because of the fact that the native data for a linked object resides separately on the disk; linking limits the object from traveling outside the local file system. Linking is actually more efficient in terms of system overhead requirements when it can be used instead of embedding.
An example of this concept is an Excel worksheet linked to a Word document. If the worksheet is moved onto a disk and taken off the local computer system, the link between the worksheet and the document is broken. Because the worksheet and document are linked, however, the Word document does not contain the native data of the worksheet--just the presentation data--so the file size is smaller, providing greater efficiency.
To link an object to another document, follow these steps:
FIGURE 16.3 Choosing Paste link in the Paste Special dialog box.
FIGURE 16.4 The linked object.
When you link an object, the linked object remains part of the original (or source) document and simply is tied to the compound document via the OLE link. Thus, whenever you update the source document, it automatically updates the compound document. Consider the steps described previously, for example. Following those steps creates a link from the Word document to the Excel data. If you now open the Excel application and change the data, the next time you open the linked Word document, the changes automatically appear in that document as well.
WARNING: If you link an object to another document, then use the object's original application to delete that object, you effectively delete the object from the container document, too.
Links can be constructed in different relationships. The following section explains these two types of relationships.
Embedding an object involves placing the object's presentation data and native data in the container document. Now the container document has all of the necessary information it needs for editing the object. Embedding makes sure that this information travels with the compound document. If the document travels to another file system, all the object's application services travel with it.
By embedding, you have placed both the native and presentation data in the container file; the file is now much larger than it would have been had you simply linked the object to the container document. Remember, when you link the object to the container document, only the object's presentation data travels to that document.
To embed an object into another document, follow these steps:
FIGURE 16.5 Creating a new object.
FIGURE 16.6 Creating an object from a file.
To edit the embedded object within the container document, simply double-click on the embedded object. You will then have access to the object's native data (toolbars). You can see that the embedded object has effectively become a part of the new or container document.
When a source document shares its data with a container document, this is termed a one-way link. Because of that link, changes to the source document automatically affect the container document accordingly.
One-way links can link an object to multiple destinations. A logo created in Microsoft Paint, for example, might be linked to an Excel spreadsheet and to a letterhead document in Word, as well as to a PowerPoint presentation.
Anytime you modify the logo in Paint, all the one-way links are recognized and updated automatically, and the new logo appears when you open the linked document, spreadsheet, or presentation.
Two-way links consist of the source document sharing its data (the object) with the destination document and the destination document sharing its own data back to the object source document (see Figure 16.7).
This can be a powerful tool that is best illustrated with an example. You could have a Word document pertaining to a sales forecast. This Word document could link to an Excel spreadsheet document. From the sales forecast in the Word document the spreadsheet in Excel creates a chart. A two-way link then links the newly created chart in Excel back to the original Word document. This two-way link means that if the source data (in Word) changes, the Excel spreadsheet is updated as well. Because the Excel chart is based on this data, Excel in turn updates the information in the chart. Finally, the two-way link updates the OLE object (the chart) in the Word document.
This example of a two-way link illustrates the strengths of two applications: Excel's capacity to create charts and Word's capacity to present narrative and data in a readable format. Examine the time-saving benefits of this particular two-way link. Because of the OLE two-way linking of information, the chart in the Word document (part of the end product) responds to any editing you make to the text data in the very same document.
After you establish the two-way link, the changes to the chart in the source document appear to occur directly; that is, you might not see Excel's intervention as changes in the Word data cause changes in the chart. (Also, note that in this scenario, the source document acts simultaneously as a container document.)
The great thing about the usefulness of OLE is that it is not necessary to have a vast knowledge of the underlying forces behind this tool. Knowing about source documents and container documents is useful to the understanding of OLE principles, but it is not essential for productive usage.
TIP: OLE's power is its capability to manipulate data between applications, without even thinking about which applications you are using. Future applications might in fact eliminate visible distinctions between applications such as word processors, spreadsheets, database applications, and so on.
Compound documents are documents that seamlessly incorporate objects of different data types from different applications. You can integrate sound clips, spreadsheets, bitmaps, and text all into a single compound document. You can create and edit these objects using their respective object applications; however, you can use OLE to integrate each of these services into the compound document. Using a compound document in this manner affords you the luxury of not having to switch back and forth between objects and applications. You can give full concentration to the compound document, avoiding the distraction of switching between documents.
You might have had the need in the past to create different types of information, by using different applications and then manually integrating the information. You could have created a spreadsheet showing sales figures, for example, then manually attached a hard copy of that sheet to a word processed narrative of your sales program.
In Windows 98 you can now seamlessly integrate these two objects from different applications, through the use of OLE technology. It is as simple as opening the spreadsheet and selecting the information you want and then dragging that information into an open word processing document. From within this newly created compound document you can edit any of the information in the document without changing applications.
Figures 16.8 and 16.9 show the ease with which you can use the drag-and-drop method to create OLE compound documents. When you select the Excel chart on the right, then drag and drop it into the Word document on the left, you get a compound document: you now have embedded the chart into the document.
FIGURE 16.8 Documents prior to drag and drop.
Windows applications that support OLE links and objects have the following commands (located in the Edit menu) to support placing and editing embedded objects:
FIGURE 16.9 Documents after drag and drop.
Note that you don't always find all of these commands in all Windows OLE applications.
In order to access object services, for example, saving the object or visually editing an object, an interface is provided. This interface provides a means for OLE to gain access to the object's native data. While interfaces are defined by OLE, they can be implemented by OLE, the object application, or the container application. The services that the interface is providing determine which application provides the interface.
OLE 2.0 provides interfaces that implement the services that are standard for all applications. Applications use these standard interfaces to make calls to the member functions. Application-, document-, or object-specific services (such as pasting from the Clipboard) are supported by interfaces implemented by the respective application. Implementation involves providing code for each of the member functions defined for the interface.
Both the container application and the object application implement interfaces that enable you to use their services or functions. If you are using the container application and want to edit an embedded object, for example, the container application makes a function call to the appropriate interface implemented by the object application. Likewise, after the object application completes an operation, such as resizing an object, it calls functions that the container application implements to change the layout of the object. The OLE library maintains communication between the container and object applications. The library intercepts calls and provides a variety of services through its interfaces. Among the many services provided by the OLE library interfaces are the storage of objects and the packaging and sending of parameters between the different process spaces.
Interfaces fall into one of four general categories:
The implementation of only a few interfaces from those in the first two areas are needed for an application developer to achieve basic OLE functionality. The specific interfaces required differ somewhat, depending on whether the application acts as a container, an object, or both. A developer can implement the appropriate additional interfaces as more features are required.
Embedded objects are stored in a hierarchical OLE storage system. This storage system is actually a miniature file system within a file, in that it implements in every compound document. The purpose of this storage system is to keep track of all the information necessary to maintain the embedded object, such as file type, native data location, presentation data location, and certain directory information.
There are two levels of storage in the OLE object storage system, categorized as follows:
OLE provides a set of interfaces through which access to the objects and data within OLE storage system is reached.
The OLE storage system provides for efficient access to the object data. This saves time and streamlines the method of access. The efficiency is carried out by the use of compound documents. Although OLE applications do not have to use compound files, compound file usage is common because the OLE object storage system provides efficient access to object data. The storage system interfaces enable objects to be read from disk to memory without loading an entire file, which is very handy when you load a compound document that contains a large number of objects or compound documents that contain a single large object such as a video clip. Loading only the data the application currently needs is more efficient because applications don't have to wait for unnecessary data to load before they can make the needed data available.
The storage system includes a two-phase commit operation specifically for those applications that require the capability to undo changes you make to a document during editing. An application that saves in Transacted mode keeps both the old and new copies of the document available until you decide to save or undo the changes. If your particular application does not need this feature, you can save in a direct mode where editing the document and its objects are incorporated as you make the changes.
Traditionally, when software vendors wanted to increase their applications' functionality, they simply created the necessary code and added it to the application's EXE file. This method of adding to the programs' functionality has two distinct disadvantages. The EXE file can become quite large, and it forces the inclusion of several other support files.
If a developer created a program for just simple word processing and wanted to add features, such as a graphing feature to create charts within the application, he had to add this functionality to the base word processor by generating additional code to the original EXE file. This makes the executable file quite large. Another software developer, wanting to add graphing capability to a spreadsheet application, would add the same functionality to that application by building onto its executable file as well.
Windows 98's OLE technology now enables application developers to implement additional features in their software without adding additional code to the original EXE file. If a word processor application and a spreadsheet application both need graphing capabilities, a mini-OLE server can be created that contains all the graphing capabilities, which both applications can share.
This technology is based on Component Object Modeling. Component Object Modeling enables developers to reduce the amount of redundant code that is used in their applications. It also enables more interoperability between applications--the different functions, such as graphing, work the same way because they are the same mini-application.
NOTE: Microsoft has released an update to the OLE model, named ActiveX. ActiveX is designed to let developers create applications to be distributed over the Internet and World Wide Web. ActiveX can be combined with COM, Win32, and Direct X multimedia services to create objects, scripts, media feeds, and applications that can be integrated in World Wide Web pages to create dynamic (or active) documents.For information on ActiveX, see Microsoft's ActiveX Resource Area Web site at http://www.microsoft.com/activex/default.htm. A few documents you might want to read for background information on ActiveX include "What Is ActiveX" and "Activating the Internet with ActiveX Technologies." The document "What Is ActiveX" explains what ActiveX is, its benefits, and who supports it. The document "Activating the Internet with ActiveX Technologies" reads like a press release, but gives you a nice overview of how ActiveX can be used in real-world scenarios.
The Component Object Model provides several advantages. First, if you need to add graphs into a container document, the applications use the same graphing OLE miniserver and apply the same user interface, reducing the need to learn two separate graphing features.
Second, each base executable file is smaller and the separate objects (OLE miniservers) are not duplicated on the hard disk, which saves drive space.
NOTE: Again, this configuration leads to greater efficiency because the separate objects are installed in a common directory so that they can be called easily from each base application. This prevents the base application from having to hunt through several directories to find the needed object.
Third, because of the reduced EXE file size, the base applications can load faster. The application does not need to load the graphing or drawing functionality until you call for it. These factors combine to give faster overall system performance.
Finally, this method facilitates better interoperability between applications. Applications that participate in this technology appear seamless, because they share the same sub-components.
The Microsoft Office package is a good example of a group of applications built on this model. MS Office contains a word processor, spreadsheet application, presentation graphics application, and database application. All of these individual applications use the same group of mini-OLE servers. For example, if you create a voice annotation in Excel (the spreadsheet application), you will use the same object that would be used if you were to create the voice annotation in Word (the word processing application). The interface that is used and the steps taken are identical for these two different applications; this is because of the fact that the mini-OLE server that is utilized is the same.
The Windows Dynamic Data Exchange (DDE) system is actually a protocol (or set of guidelines) that enables DDE-compatible Windows applications to share data easily with other compatible applications. You can use DDE to perform one-time data transfers or ongoing conversations. In ongoing real-time conversations, applications send updates to one another as new data becomes available.
You should first familiarize yourself with the following basic DDE terms in order to better understand DDE concepts (and the examples later in this chapter):
| Application | DDE Application Name |
| Microsoft Access | MSAccess |
| Microsoft Excel | Excel |
| Microsoft FoxPro | FoxPro |
| Microsoft Project | Project |
| Microsoft Word for Windows | WinWord |
All DDE communications occur in a channel between applications. DDE functions Initiate and Terminate open and close the channel. The client application controls the channel and requests services from the server.
Word for Windows will be used in the following discussion to serve as the sample application from which transactions are issued. Most other DDE- compatible applications function similarly. There will be examples from other applications that follow later in the chapter.
The Initiate function opens a DDE channel from the client application to the server application. This function has two parameters: the server application name and the topic. The application name is the server program name minus the EXE extension; for example, Excel. (If the server application is not running, the Initiate function launches the application.)
Initiate returns a channel number to the client application (in addition to opening the channel). The channel number is used as a parameter in all other DDE functions subsequent to identifying the channel.
Calling the Initiate function several times can open more than one channel. Each channel is numbered after the Initiate function is called. An error message will appear if the server application is not running and cannot be started. The same error message will appear if the topic is not valid.
Additional DDE functions include Terminate, Request, Poke, and Execute, summarized in the following list:
The client and server applications must be running before a DDE conversation can occur. Therefore, a macro to initiate a DDE conversation usually should include instructions that carry out the following three steps:
You can use the AppIsRunning() function to determine whether an application is running, using the following syntax:
AppIsRunning(WindowName$)
WindowName$ is the name of the application as it appears in the title bar of the application window. For example, you would use the following syntax to determine whether Microsoft Excel is running:
status = AppIsRunning("Microsoft Excel")
WARNING: The WindowName$ for an application is not the same as the DDE application name.
If the server application is not running, you can use the Shell statement to start it. The Shell function requires the actual application filename with the extension; for example, EXCEL.EXE. (If the application you want to start is not in the current directory or path, you must specify the path as well as the filename.) For example:
Shell "C:\Excel\Excel.exe"
To open a document at the same time you start the application, you can add a parameter that specifies a document filename with the application file- name or just the document filename, assuming the filename extension has been associated with the application you want to start:
Shell "C:\Excel\Examples\Budget.xls"
Here is another example of how you might use AppIsRunning() and Shell together:
If AppIsRunning("Microsoft Excel") = 0 Then Shell "Excel.exe"
After you establish that the application you want to use as the server is running, you can use DDEInitiate() to initiate the DDE conversation, as follows:
DDEInitiate(Application$, Topic$)
Application$ is the DDE application name of the application with which you want to initiate a conversation. Topic$ is the name of a topic the application currently supports. For example, the following instruction initiates a conversation with Microsoft Excel on the System topic:
chan = DDEInitiate("Excel", "System")
If DDEInitiate() successfully initiates a conversation with the specified server application and topic, it returns a channel number. You then can use this channel number as an argument in other DDE statements and functions to refer to this particular DDE conversation.
An error occurs if the application is not running or if the application does not recognize the topic. If you specify Microsoft Excel as the application name and BUDGET.XLS as the topic, but BUDGET.XLS is not open, for example, Windows 98 generates an error.
Now that you have initiated a DDE conversation with another application, you can use the DDERequest$() function to obtain information from an item within the specified topic, using the following syntax:
DDERequest$(ChanNum, Item$)
ChanNum is the number of a channel the DDEInitiate() function returns. Item$ is an item the DDE conversation's topic supports.
The following is an example of a DDERequest$() to query the System topic in Microsoft Excel to produce a list of the currently supported topics:
If AppIsRunning("Microsoft Excel") = 0 Then Shell "Excel.exe", 1
chan = DDEInitiate("Excel", "System")
topics$ = DDERequest$(chan, "Topics")
"Topics" is an item in the System topic that lists all the topics currently available. You can add a MsgBox instruction if you want to display the list of topics in a message box.
Topic names are separated by tab marks, which appear as spaces in the message box. For example, C:\ Excel\ Examples\ Amortize.xls, C:\ Excel\ Examples\ Budget.xls, and Sheet2 are the names of open Microsoft Excel documents that can be accessed as topics in DDE conversations.
If the specified channel number does not refer to an active DDE conversation, DDERequest$() generates an error. You also get an error if the other application does not recognize the specified item.
Note that because DDERequest$() is a string function, it always returns information to the Word macro in the form of a string.
Although the client in a DDE conversation usually obtains information from the server, the client also can send information to the server, using the DDEPoke statement with the following syntax:
DDEPoke ChanNum, Item$, Data$
ChanNum is the channel number returned by the DDEInitiate() instruction that began the DDE conversation. Item$ is the name of an item supported by the DDE conversation's topic. Data$ is the information, in the form of a string, that you want to insert into the item. (All numbers must be first converted to strings using the Str$() function.)
The following example "pokes" the numeric value 100 into the first cell of the Microsoft Excel worksheet that is the topic of the DDE conversation. The Str$() function converts the value into a string:
DDEPoke chan1, "R1C1", Str$(100)
You use the DDEExecute statement to send a command recognized by the server application:
DDEExecute ChanNum, Command$
ChanNum is the channel number DDEInitiate() returns.
In Microsoft Excel and many other applications that support DDE, Command$ is a statement or function in the application's macro language. For example, in Microsoft Excel the XLM macro statement that creates a new worksheet is NEW(1).
To send the same command through a DDE channel, use the following:
DDEExecute chan1, "[NEW(1)]"
Most DDE applications, such as Microsoft Excel, require that each command received through a DDE channel be enclosed in brackets. You can send more than one command through a single DDEExecute instruction by enclosing each command in brackets. For example, the following instruction instructs Microsoft Excel to open and close a worksheet:
DDEExecute chan1, "[NEW(1)][FILE.CLOSE(0)]"
Sending multiple commands can speed up the DDE macro a great deal. The preceding instruction is equivalent to the following two instructions:
DDEExecute chan1, "[NEW(1)]" DDEExecute chan1, "[FILE.CLOSE(0)]"
Some commands require arguments in the form of strings enclosed in quotation marks. Because the quotation mark indicates the beginning and end of a string in WordBasic, you must use Chr$(34) to include that quotation mark in a command string. In order to send the Microsoft Excel macro instruction OPEN("Sales.xls"), you would use the following instruction:
DDEExecute chan1, "[OPEN(" + Chr$(34) + "Sales.xls" + Chr$(34) + ")]"
DDE channels do not close automatically until you exit the client application. If you do not close a channel, it remains open, even after the macro ends. Because each channel uses system resources, you should always close channels after you no longer need them to improve overall system performance.
You use DDETerminate to terminate a DDE conversation, as follows:
DDETerminate ChanNum
ChanNum is the channel number DDEInitiate() returns.
After you close Word, it automatically terminates all active DDE conver-sations. However, you might want to terminate all conversations without closing Word. WordBasic includes the DDETerminateAll function as a shortcut to close channels one by one. DDETerminateAll terminates all active DDE conversations that Word initiates. Still, it does not terminate DDE conversations that another application might have initiated with Word as the server.
DDE actually is the foundation on which OLE technology is built. You can perform many DDE functions more easily by using OLE. An example would be using the "paste-link" process described in Chapter 16, "OLE, DDE, and File Associations," to establish a link between a word processing document and a spreadsheet, instead of including a macro (such as the one described earlier in this chapter) in your document. You might find this more convenient most often.
Situations might arise, however, particularly if you engage in even basic application programming, in which you need to exercise more control over the link between applications. You might then find DDE better suited to the task. Additionally, OLE can tie up substantial amounts of your system's resources in establishing links. Using DDE enables you to exercise more control over the data exchanges and, in the process, determine the appropriate levels of data exchange.
File Association works to tell Windows 98 that a particular filename ex- tension is linked to a specific program. When a file is opened, it has an extension associated with a given application; the operating system automatically starts the application and then loads the file. To open an application, you double-click a related document file, and the file's type must be defined in the Window 98's Registry to be valid. As such, it appears in a list of file types that you can associate with an application.
For more information about associating a file type with a specific application so that the application runs as you double-click a file, see the online Help.
If a file type has been associated with an application, you can re- associate the file type with a different application. The steps involved in re-associating a file are listed below:
In some applications, such as Microsoft Excel, you can associate multiple extensions with a file type. For example, a Microsoft Excel file is associated by default with an .XLS extension. Problems may result if the user attempts to change which application opens a particular file type. To re-associate a file type with an application in this situation, you are required to delete all extensions registered to that application. You must then re-associate each file type with an application. In addition, you must also redefine the Open, Print, and DDE commands for each file type. To do this, in My Computer or Windows Explorer, click the View menu, click Options, and then click the File Type tab.
FIGURE 16.10 The application used to perform an action.
If you select New from the File menu when in Windows Explorer or in the Context menu, a list of objects appears. For example, a folder or Microsoft Excel 5.0 worksheet may be listed. Clicking an object creates a new object in Windows Explorer or on the desktop. You can add an object to this list by using the Registry Editor and adding a key called ShellNew to the related file extension for the related filename extension:
Hkey_Classes_Root\.ext
After you have created the ShellNew key, you will need to add a string value called FileName. That string value will have a data value that is the same as the path name to a template file in the ShellNew subdirectory. For example:
FileName="c:\windows\shellnew\excel.xls
© Copyright, Macmillan Computer Publishing. All rights reserved.