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,
As you've probably guessed, the second statement, which calls the built-in VB function Get behind the WheelNow 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 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
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
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 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
LocationURL: Although it's similar to LocationName, 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 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/ 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
Error 440 occurs when IE objects to being asked for a particular property value. To avoid error 440, place the statement 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 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 the manager of WINDOWS Magazine Online on America Online and CompuServe. Reach Karen care at the e-mail address here.
|