[ Go to July 1997 Table of Contents ]

Power Windows /
Karen Kenworthy
Karen Kenworthy

Steer It Toward the Web

Have you noticed how everything is becoming Web-enabled? It seems like each new program has special Internet features. Word processors and spreadsheet programs are now Net-ready. Even television sets and other household appliances are jumping on the Internet bandwagon.

Soon, anything that can't browse the Net will seem obsolete. If you don't want to be left behind, I've got some good news for you. You can add Internet functions to even the simplest macro or program without much trouble. All you need is a copy of Microsoft's Internet Explorer (IE) and a programming or macro language that supports ActiveX Scripting.

It's easy to get a copy of Internet Explorer if you don't already have it. Download it from Microsoft's Web site at http://www.microsoft.com-you can even get a Windows 3.x version.

Languages that support ActiveX Scripting (formerly known as OLE Automation) are also easy to find. Popular applications such as Microsoft Word and Excel include a macro language such as Visual Basic for Applications (VBA) that allows you to add or expand the app's Internet features. You can also use most end-user programming languages, such as VBA's big brother Visual Basic (VB) and Borland's Delphi, to quickly write completely new programs with full Internet support.

For our examples, I've chosen VB 3.0. Even though this older language produces 16-bit programs, it can be used to control newer 32-bit apps like IE. And with few-if any-changes, the statements we'll see in a moment also work with projects written in other languages, too. So grab a copy of IE, and let's get started.

Like many computer discussions these days, this one begins with talk of objects. As you may recall, objects are really just special programs. Like programs, objects contain variables (places where data is stored) and subroutines (instructions that describe what the computer should do to the object's data)

Some of an object's subroutines and variables may be private. In that case, only the object can use them. Other programs or objects cannot see or access private subroutines and variables.

But every object exposes at least one subroutine or variable. In everyday usage, the word "expose" means to "make visible or accessible." Believe it or not, this is one of those rare occasions when computer programmers and designers give a word almost the same meaning you and I would. Exposed subroutines and variables aren't hidden or private. Other programs and objects can see and access them. That's how objects communicate with the rest of the world.

You may not have thought of it before, but IE is an object. It has thousands of private subroutines and variables. It also has dozens of exposed methods and properties. These methods and properties make it possible for other programs to control IE and to monitor its activities.

In VB, it only takes two statements to take control of an object like IE. The sidebar "Launch Pad" shows how it can be done. The first statement creates a variable, objExplorer, where information about the IE object will be stored. Notice that the type of the variable must be Object.

As you've probably guessed, the second statement, which calls the built-in VB function CreateObject, creates an object. The type of object, InternetExplorer.Application, is passed to CreateObject via the function's one and only parameter. In response, CreateObject launches a new copy of IE, obtains initial information about the program just run, then stores that information in our Object variable.

Get behind the Wheel

Now that we've created and initialized an IE object, let's take control of it. As you'll recall, we do that by accessing the object's properties and methods.

VB programs use an object's methods and properties the same way they use their own subroutines and variables. However, object methods and properties have special two-part names. The first part is the name of the object variable that contains the object's information. The second part is a unique word that describes the method or property's purpose. The two parts are separated by a single dot (".")

To see this in action, take another look at our "Launch Pad" sidebar. Its third statement makes our IE object's main window appear by storing a value of True in objExplorer.Visible. From its name, we know we're modifying the Visible property of the object whose information is stored in objExplorer. Changing the property's value back to False (IE's main window is invisible when first created) would make the window invisible again.

There are lots of other tricks we can make IE perform. The following is a list of the most useful IE properties, and the information contained:

ToolBar: Like Visible, you can also set this property to either True or False. True (the default value) causes IE to display its toolbar, including the text box where the user may type URLs. Storing False in this property causes IE's toolbar to disappear.

StatusBar: The value stored in the property determines whether IE shows its status bar (the area at the bottom of IE's main window where messages are displayed). If the property has a value of False, the status bar disappears. The default value is True.

MenuBar: This property allows you to determine whether IE displays its menu bar. If the property's value is True (the default), the menu bar is visible. Otherwise, IE hides its menu bar. By setting MenuBar, ToolBar and StatusBar to False, you can cause IE to display a window containing only a title bar and the contents of the current URL. This gives you complete control of IE; no user can steer it to another URL, change any settings or interact with it in any other way.

Busy: IE controls this property's value. It's True while IE actively navigates to a new page or retrieves a file; it's False when IE is idle.

StatusText: This property contains the text that is currently being displayed in IE's status bar. Retrieve the property's value to determine what's being displayed, or store a new value to change the status bar message.

LocationName: The name (usually a Web page's title) of the URL currently being displayed by IE is contained here. This property is read-only (you can retrieve its value, but only IE can change it). To change it, see the Navigate method.

LocationURL: Although it's similar to LocationName, LocationURL contains the actual URL of the page currently being displayed by IE, not its title or name. It's also read-only. To change this property, see the Navigate method.

IE also exposes several methods. Calling a method causes IE to perform a specific action. Like other subroutines, some methods accept parameters, and some return values. Here are some of the more interesting methods, and a short description of what they do:

Navigate: This method causes IE to display a new URL. The URL is passed to the method as a parameter. Like URLs you enter when you run IE manually, the URL passed to Navigate doesn't have to be an http (Web page) link. You can also specify ftp, File (local disk file) and even mailto (send e-mail) URLs.

Refresh: Like clicking on the Refresh button on IE's toolbar, this method causes the program to reload all data retrieved from the last URL.

GoBack: Calling this method causes IE to display the previous URL. It has the same effect as clicking on the Back button on IE's toolbar.

GoForward: Only valid after at least one GoBack method, the GoForward method causes IE to display the URL originally displayed immediately after the current URL. This method has the same effect as IE's Forward toolbar button.

Stop: Calling this method has the same effect as clicking on IE's Stop toolbar button.

Quit: This method has the same effect as selecting Exit from IE's File menu.

Microsoft has prepared a document, in Word format, that provides more information about all of Internet Explorer's methods and properties. Find a copy at http://www.microsoft.com/workshop/prog/sdk/docs/iexplore/other/iexplore.zip.

While visiting the WinMag site, pick up a copy of the WinMag Surfer. This VB program uses many of the properties and methods we've just discussed. The archive (PWSURF.ZIP) includes both the source code and executable file, so you can use the Surfer as a starting point for your own Web experiments.

Gotchas: If you decide to write a Web-enabled program of your own, your program may occasionally stop unexpectedly and report an OLE Automation Error (VB error number 440) or Object Variable Not Set (VB error number 91)

Error 440 occurs when IE objects to being asked for a particular property value. To avoid error 440, place the statement On Error Resume Next ahead of the statement where the error occurred. VB will now continue running your program even over IE's objections. If a particular property value is not available, the statement that attempted to retrieve it will be skipped. If your program needs to know this has happened, place statements that test the value of the built-in VB variable Err, which will contain the value 440 if an error has occurred.

Error 91 occurs less frequently than error 440, but it is just as easy to prevent or handle. This error occurs when your program tries to use an object variable that has no value.

The On Error Resume Next statement can also be used, to allow your program to continue even when error 91 occurs. As before, the offending statement is skipped. However, this time the Err variable will contain the number 91.

Karen Kenworthy is the author of Visual Basic for Applications, Revealed! (Prima Publishing, 1994), a nonprogrammer's introduction to VBA. She is also a contributing editor to WINDOWS Magazine and manager of WINDOWS Magazine Online on America Online and CompuServe. Reach Karen care of the editor at the addresses on page 20.


Windows Magazine, July 1997, page 249.

[ Go to July 1997 Table of Contents ]