Hour 30


During this hour you will learn

Enhancing Programs with ActiveX

This lesson explains how to use ActiveX controls. You may not have realized it, but you've been using ActiveX controls ever since Part I of this tutorial. ActiveX technology is integral to Visual Basic and to almost every other current Microsoft product, especially Microsoft's Internet-related products such as Internet Explorer. An ActiveX control is nothing more than a control like the Common Dialog Box control you used to build dialog boxes in Hour 17.

A general overview of ActiveX is simple because it's a replacement for OLE as well as for the Visual Basic controls you add to your Visual Basic development environment. If all you did was implement simple OLE as described in Hour 22 "Adding OLE to a Program," insert new controls you purchase or obtain online, and insert within VB's Components dialog box, ActiveX would remain simple. The difficult part of ActiveX appears when you begin tackling the programming behind ActiveX-when you want to create your own controls to add to the development environment, for example.

ActiveX's simple-but-difficult nature prompted two ActiveX lessons in this book. This lesson explains ActiveX in detail and demonstrates uses of ActiveX controls. The next lesson walks you through the somewhat challenging task of creating your own ActiveX control.

FAST TRACK
Have you ever tried to create an ActiveX control? Visual Basic 5 makes creating such a control much easier than tools that came before. You learn how to create an ActiveX control in next hour's lesson, "Creating a Simple ActiveX Control".

Topic 1: ActiveX Explained

Before programming ActiveX, you must master ActiveX objects and learn how to use them in programming applications from Visual Basic to end-user applications such as Microsoft Word. ActiveX is today's buzzword in computing and Microsoft supports quickly adopting the ActiveX technology, just as the development community has.

Overview

This topic section explains ActiveX in more detail. This lesson and the next primarily deal with ActiveX from a VB developer's point of view, but this introductory topic section gives you a better ActiveX understanding for both users as well as developers.

Benefits

Before getting into the details, here is one of the primary benefits of ActiveX: You can drop (sometimes with drag-and-drop and sometimes by using more procedural methods) ActiveX controls into not only Visual Basic, but also Excel, Access, and many other products. The product doesn't have to be written by Microsoft. ActiveX is an upward-compatible replacement for OLE and OCX controls. Almost every Windows program now supports ActiveX in some way. Although different products support the inclusion of ActiveX differently, the bottom-line reason for using ActiveX is that you can insert ActiveX controls in virtually any program you use.

A general rule of thumb is that ActiveX controls take the place of Visual Basic controls that used to be known as OCXs (all ActiveX controls retain the OCX file-name extension). The use of ActiveX replaces OLE that used to be the primary source of inter-object communication in Windows applications.

You can no longer use VBX controls, commonly known as Visual Basic controls, inside Visual Basic 5. OCX ActiveX controls completely replace VBX controls because VBXs are 16-bit only, whereas ActiveX controls are exclusively used in 32-bit environments. If you tried to use a VBX, Visual Basic would issue an error message and wouldn't compile your code until you remove the control from the application.

Already, many vendors supply stand-alone ActiveX technology tools. Therefore, as a Visual Basic developer, you can obtain numerous ActiveX controls that you can use immediately in Visual Basic's development environment. After you put the ActiveX control into the Toolbox, the control works just like the intrinsic controls that already appear in the Toolbox. You can set ActiveX properties, respond to ActiveX events, and trigger ActiveX methods.

Today's Internet users benefit dramatically from ActiveX technology if they use an ActiveX-aware Web browser such as Internet Explorer 3 or Netscape Navigator 3 with its ActiveX support plug-in. You can embed ActiveX controls inside Web pages that you create. When other Internet users view your Web page with an ActiveX-aware Web browser, they will see active content instead of a static Web page. Depending on the ActiveX control's capabilities, Web users can interact with the ActiveX control and see results as though the Web page contained a small Visual Basic program in addition to the normal text and graphics.

ActiveX controls are reusable. After you create an ActiveX control in Visual Basic, you can use that control in every other programming platform that also supports ActiveX, such as Visual C++, Visual J++, Borland Delphi, Borland C++, or Java.

The Internet isn't the only reason to use ActiveX, but it's the original reason Microsoft transformed the OLE and OCX technology into ActiveX. OLE controls couldn't function inside a Web page as can ActiveX controls. By tweaking OLE just a bit, ActiveX controls can fully support OLE and OCX technology while extending that technology further.

ActiveX for Java Programmers

If you've never heard of Java (the computer-based Java, not the coffee or the Indonesian island), it's a new technology that activates Web sites. A Java application is an embedded in-place applet (a small application) that you insert into a Web page so that users can interact with the Web page and with interactive technology such as multimedia. Of course, that's exactly what the previous section said about ActiveX! ActiveX isn't technically called a replacement for Java applets, but in almost every case, you can replace a Java-based applet with an ActiveX control. What's more, the ActiveX control can be more powerful than the Web-based Java language.

Java programmers can extend their Java Web-based programs with ActiveX controls. In reality, many of today's Java programmers simply write enough Java language to wrap around an ActiveX control. The control remains the focus of the Java applet, even though the ActiveX control may be written in a non-Java language such as Visual Basic or C++.

This and next hour's ActiveX lesson won't discuss the Internet much further. Hour 32, "Adding Internet Access to Applications," is devoted entirely to Visual Basic's Internet connection.

Example

If you have Internet access, why not browse a few ActiveX-based sites? Point your ActiveX-aware Web browser to Intel's interactive ActiveX site at http://www.intel.com (see Figure 30.1). The site contains ActiveX controls that display moving pictures and color-changing icons.

FIG. 30.1

View ActiveX content on Intel's home page.

You'll also find some fun ActiveX controls on United Media's Web page at http://www.unitedmedia.com (see Figure 30.2). The Web site contains several popular cartoons, and the ActiveX content helps to bring some of the cartoons to life.

FIG. 30.2

The United Media's site includes several fun ActiveX-based controls.

Next Step

Before a Web site can display ActiveX content on your computer, the ActiveX control must download from the site to your Web browser. Therefore, when you connect to a Web page that requires an ActiveX control to view, the Web site checks to see whether your computer has the needed ActiveX control. If your computer doesn't have the control, the Web site sends it to your computer. (Some security measures are in place to ensure that damaging programs don't get on your machine.)

Check out Microsoft's ActiveX site (http://www.microsoft.com) for a categorized sample of ActiveX controls that you can download to your computer. When there, the ActiveX control will be in place for subsequent Web sites you visit that require the controls. Microsoft's ActiveX site not only lets you download the controls, but also lets you view the control's features before you download them.

You'll be amazed at some of the things ActiveX controls can do. ActiveX controls act like complete Visual Basic applications in some cases because they can be extremely complex and powerful.

Topic 2: Integrating ActiveX Controls

When you add tools to your Toolbox, those tools are almost always ActiveX controls (for Visual Basic 5). These tools become part of the Toolbox and part of the development environment just like the standard intrinsic controls. After you add the ActiveX control to the Toolbox, you can select and place that control on the Form window just as you place controls such as command buttons.

Overview

ActiveX controls let you extend Visual Basic and even turn VB into a customized programming environment that suits your exact needs. Suppose that you specialize in multimedia programming and use Visual Basic as your development platform to create multimedia applications. Over time, you'll collect multimedia-based ActiveX controls. Although Visual Basic comes with extra multimedia ActiveX controls, other vendors might supply specialized video-coloring controls, video playback tools, and video editing tools. You'll add these tools to your Visual Basic environment, and perhaps eliminate some controls from the Visual Basic directory that you'll rarely, if ever, use.

Be Careful with Older Controls

If you've worked with previous editions of Visual Basic and added several 16-bit VBX controls, the current 32-bit Visual Basic system can't use those controls unless the system comes with 32-bit replacements for those old controls. For example, Visual Basic comes with a 32-bit version of the Gauge control. (16-bit Gauge VBX controls have been around for several versions of Visual Basic.) Therefore, if you load an older VB application that uses the Gauge VBX control, Visual Basic will replace that control with the 32-bit version and things should work fine. If, however, a replacement doesn't exist and the VBX vendor can't supply you with a replacement, you'll have to eliminate that control from the application and substitute a similar one.

Distributing ActiveX Controls

When you distribute your applications to users, you must distribute a runtime copy of the application's ActiveX controls as well. These ActiveX controls go to each user's \Windows\System or \Windows\System32 folders. Therefore, when you create a setup and installation routine for the applications you distribute, you must take into account any external ActiveX controls (those outside the realm of intrinsic controls) your application contains.

Hour 37, "Packaging your Application," explains more about setting up an application for distribution and installation.

Example

Although your system may contain several ActiveX controls, Visual Basic doesn't load these controls into its Toolbox window because, by default, only the intrinsic default tools appear there. Also, Visual Basic doesn't automatically locate all ActiveX controls on your system during installation.

Nevertheless, Visual Basic's installation routine does install and locate several ActiveX controls, which you can add to your development environment's Toolbox window. Here's a review of the steps you take when installing ActiveX controls into your Visual Basic's development environment:

  1. From the Project menu, choose Components to open the Components dialog box (see Figure 30.3).

FIG. 30.3

Add controls to your Toolbox from the Components dialog box.

  1. Click the check box next to each ActiveX control you want to add to your project.
  2. Click OK to close the dialog box. The Toolbox now contains the new controls.

Some ActiveX control groups that you select-such as Microsoft Payment and Address Selector, available with the Professional Edition-contain more than one tool, so multiple tools may appear on the Toolbox after you select just a single ActiveX control title.

The ActiveX control's path name and disk name (with an OCX extension) appear in the Components dialog box when you select a control.

The Components dialog box includes three tabs. You'll use the first tab, Controls, most of the time because you'll be adding ActiveX controls to your form. The Components dialog box also supports a Designers tab that lets you add designers, or class descriptions with which you can build new classes of objects. If you use the Professional or Enterprise editions, the Designers tab will show designers for ActiveX controls and documents.

The Insertable Objects tab supplies a list of registered insertable objects that you can drop into an application. An insertable object usually comprises more scope that the typical control supports; for example, you can insert an Excel worksheet in the middle of your Form window.

The insertable feature demonstrates ActiveX's upgraded and close relationship to OLE technology. The insertable object's icon appears on your Toolbox window when you click OK in the Component dialog box. However, the insertable object becomes much more active and typically requires that you select a data document, such as a worksheet or graphic image, before the object appears in your Form window. For example, if you insert the insertable object named Microsoft Clip Gallery, the Microsoft Clipart Gallery application executes and lets you select a graphic, sound, or multimedia clip, to insert into your application. In other words, the program itself (the Microsoft Clip Gallery) doesn't go into your application, but the program's compatible data file does. The insertable object, therefore, executes rather than embeds itself into your application.

Don't add too many controls at one time until you're sure that you need all of them. Figure 30.4 shows the result of adding far too many controls. The Toolbox window is too big to be useful. If it turns out that you need so many controls at one time, you'll have to hide and display the Toolbox (use the View menu) so that it stays hidden until you need it.

You also can right-click the Toolbox and choose Add Tab to convert the Toolbox into a more organized set of pages. You can then place similar tools on their own pages to group them together.

FIG. 30.4

Don't add too many ActiveX controls to your Toolbox.

Next Step

Suppose that ActiveX controls appear on your system in folders other than Visual Basic's primary search folder. When you choose Components from the Project menu, the Components dialog box can't locate the ActiveX controls without your help. Therefore, if you don't see the ActiveX control you want to add to your project, click the dialog box's Browse button to locate the OCX files you want to load. When you double-click a located file, Visual Basic adds that OCX to the Toolbox.

Topic 3: Working with ActiveX

After you add the controls, you know all about setting properties, responding to events, and triggering methods. Some ActiveX controls let you go a step farther, however. You can actually use an embedded control inside your application and borrow that control's functionality for your own application's use.

Overview

In Hour 22, "Adding OLE to a Program," you learned some about OLE but were told that ActiveX replaced OLE. You also learned that OLE, in its truest form, exists only in Visual Basic's Standard Edition because other than a few drop-in controls, ActiveX has little support in Visual Basic 5. Although ActiveX is a replacement for OLE, programmers still use OLE terminology when describing such topics as Automation.

When your application uses Automation, your application processes another application's data and manipulates that data with the help of the other application. Users will have no idea that the other application has started, helped, and then gone away. For example, you can open Excel, load a worksheet, manipulate the worksheet data with Excel-based commands, close Excel, and embed the resulting worksheet in your application's Form window without users ever knowing that you borrowed Excel's capabilities.

This topic section is comprised primarily of a long example because you'll best grasp Automation when you see it in action. You're somewhat limited to using Automation only for ActiveX applications registered in your system Registry. Generally, if you use an application that's ActiveX-aware, that application registered its Automation availability in your system Registry when you installed the application.

Don't expect to master Automation in a single topic section. Automation requires extensive knowledge of the other application's object hierarchy, and such a hierarchy can be complex. The following example demonstrates Automation with an ActiveX document more than it teaches Automation due to Automation's requirement that you be well versed in the borrowed application's internals. This course can't get into the specifics of other applications. Fortunately, most Automation concepts overlap applications, so the example's concepts you see here carry over to other applications in many ways.

Example

You'll use Visual Basic's Object data type to create a variable that references the Automation application. First, you must define an application object like this:

Dim obExcelApp As Object

You must connect the application object variable to the application. If the application isn't running now, you must start the application in the background with the CreateObject() function. CreateObject() not only starts the application, but also connects your object variable to the application like this:

Set obExcelApp = CreateObject("Excel.Application")

You'll substitute the application's name in place of the CreateObject() function's argument.

Be sure to use Set and not a simple assignment. A variable can't hold an outside application, so you use Set to create a reference variable to the outside object.

A problem can occur if the application is already running. As a multitasking, multiuser operating system, Excel can have more than one copy of itself running at the same time on the same computer. Therefore, you can use the GetObject() function in place of CreateObject() if the application is running like this:

Set obExcelApp = GetObject(, "Excel.Application")

Notice the comma at the beginning of the argument list. You omit the first argument in most cases. You can check for failure with the following If:

If obExcelApp Is Nothing Then

  ' You must use CreateObject()...

Automation application object variables are an exception to the rule of local variables. The application is truly outside your application, so you could safely use a global object variable so that your procedures don't have to pass the application variable around.

When you open another application and use Automation, your application must intimately understand the other application's interface. In a way, your application is the user of the other application. Therefore, when you open the Excel application, you interact with Excel by using the normal row and column notation, except that you have to use some object property notation specific to Excel.

Now you must declare a worksheet object so that the application can generate data:

Dim obWorkSheet As Object  ' Worksheet object

The following code adds data to some worksheet cells:

' Enter values in cells

obWorkSheet.Cells(1, 1).Value = "Sales"

obWorkSheet.Cells(1, 2).Value = "Month"

obWorkSheet.Cells(2, 1).Value = 21913.44

obWorkSheet.Cells(2, 2).Value = "April"

Next Step

If you put everything together that you saw in the preceding section, as well as add some cleanup code and code that expands the end of the code to save the worksheet and closes the Excel object, you would come up with something like Listing 30.1.

Listing 30.1 Automat.bas: Using Excel to Create a Worksheet

Dim obExcelApp As Object   ' Application object

Dim obWorkSheet As Object  ' Worksheet object

' Set the Excel application reference

Set obExcelApp = CreateObject("Excel.Application")

' Add a new workbook

obExcelApp.Workbooks.Add

' Reference the active sheet

Set obWorkSheet = obExcelApp.ActiveSheet

' Enter values in active sheet's cells

obWorkSheet.Cells(1, 1).Value = "Sales"

obWorkSheet.Cells(1, 2).Value = "Month"

obWorkSheet.Cells(2, 1).Value = 21913.44

obWorkSheet.Cells(2, 2).Value = "April"

' Select the second row only to format

obWorkSheet.Rows("2:2").Select

obExcelApp.Selection.NumberFormat = "$##,###.##"

' Save the worksbook

obExcelApp.Save ("c:\temp")

' Don't quit if Excel was already running!

obExcelApp.ActiveWorkBook.Close False

If blnNotRunning Then

  obexcelApp.Quit   ' Quit Excel

End If

If you tested to see that Excel wasn't already executing, you can close Excel as done in this final statement. If, however, Excel was running (meaning that the GetObject() function didn't come back as Nothing), don't quit the application, because it might be running a background process. The code shown in this quick example creates the simple worksheet shown in Figure 30.5.

FIG. 30.5

Your Visual Basic application can create Excel worksheets!

Although the worksheet is simple, it's simple just to make the example a reasonable size. Ordinarily, your Visual Basic application would adjust values and perhaps trigger an Excel chart and print a report. The important thing to remember is that Visual Basic used Excel's brains to create a formatted worksheet without users sitting at the keyboard knowing that Excel was involved.

Excel contains its own Automation language, as does Word and all of the ActiveX-aware, Automation-compatible applications. Nevertheless, most of the applications support the opening and closing features that were used to connect an application and the application's primary data object to Visual Basic object variables in this topic section. Also, the applications support methods and properties such as the ones shown here, so you'll have little problem as long as you understand Visual Basic and have access to the application's internal language used for Automation. Search the application's online help for the application's object hierarchy, which shows the available objects you can work.

After you use Excel or Word or some other ActiveX-compatible Automation application, you then have that object in your Visual Basic application.

Summary

You now understand ActiveX more fully, especially how ActiveX relates to VBX, OCX, and the OLE technologies. ActiveX is just an evolved replacement for those older technologies. You can drop ActiveX controls into VB's development environment and control outside applications from a simple Visual Basic program. Application control through Automation lets your Visual Basic application generate data by borrowing the usefulness of an outside application (even if the application is already running in another window) to create the data. Users at the keyboard won't even know that your application uses the other application's functionality. This reuse is one of ActiveX's benefits because after you create a control, you can use that control in many subsequent programs that you write.

In next hour's lesson you'll see how to create an ActiveX control with Visual Basic. Such a task can be daunting, but you're ready to take on the challenge!

Hour 30 Quiz

  1. What does ActiveX replace? (Hint: At least three answers are possible.)
  2. Where can you add new ActiveX controls?
  3. What happens if the Components dialog box can't locate the ActiveX control that you want to add to your Toolbox?
  4. What can you do if you want to make some old 16-bit VBX controls work in Visual Basic 5?
  5. What does Automation mean?
  6. True or false: If you sell your application, you must properly install the application's ActiveX controls on the user's system.
  7. Why can't you directly assign applications to object variables?
  8. What happens if your application uses CreateObject() for a Word document and Word is already running?
  9. How can you determine the Automation application's object and property names?
  10. What do users see when your application performs Automation?

Hour 30 Homework Exercises

  1. Use the Components dialog box to search your disk for ActiveX controls; you'll probably find some that aren't in the Windows folder. For example, if you're a member of Microsoft Network's online service, you'll find several ActiveX controls in the Microsoft Network folder.
  2. Write an Excel worksheet-opening routine that checks to see whether Excel is now running. If so, use GetObject() to get the Visual Basic worksheet object; if Excel isn't open, open Excel with the CreateObject() function. Add message box error messages if needed.
  3. What's the difference between an insertable object and an ActiveX control that doesn't appear on the Components dialog box's Insertable Objects page? Insert one and see what happens when you use it.

© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.