Using Internet Explorer Objects

Microsoft Internet Explorer is a general-purpose browser application that displays HTML documents located on the Internet, corporate intranet, or your local hard disk. Although Internet Explorer doesn't have its own Visual Basic Editor, you can use its objects through Automation by manipulating a simple collection of properties, methods, and events.

The Internet Explorer object library typically resides on all systems that maintain an installed copy of Internet Explorer. (In other words, if Internet Explorer is properly recorded in the system registry, an object library will also be present.) Because Internet Explorer is included with Office 2000, you have everything you need to issue Internet Explorer commands from within an Office macro. By writing just a few lines of code, you can quickly start Internet Explorer and display any Web page on the Internet that you have access to.

NOTE
Unfortunately, Netscape Navigator has a different object model and therefore can't be controlled with the Automation commands shown here.

Creating the WebPage Macro

Follow these steps to create the WebPage macro, a Word utility that uses Internet Explorer to open the Microsoft Corporation home page in Internet Explorer. (You can substitute your own Internet address for the Microsoft address if you want.)

ON THE WEB
The WebPage macro is located in the Chap62 document on the Running Office 2000 Reader's Corner page.

  1. In Word, choose Macros from the Macro submenu of the Tools menu.
  2. Type WebPage, and then click Create.
  3. From the Visual Basic Tools menu, choose References.
  4. Select the Microsoft Internet Controls reference (SHDOCVW.DLL) and place a checkmark in the check box. Unlike the other Office applications, Internet Explorer requires this special object library to run commands.
  5. Click OK to add the reference to your project. Visual Basic adds the Internet Explorer object library to your project.
  6. Type the following code in the WebPage macro using the Code window:
  7.  Set Explorer = New SHDocVw.InternetExplorer  Explorer.Visible = True  Explorer.Navigate "http://www.microsoft.com" 

  8. Click the Save button to save the macro to disk.

This macro uses an object variable named Explorer to represent the InternetExplorer class in the SHDOCVW.DLL object library. The macro then starts and displays the Internet Explorer application in a window and uses the Navigate method to open the Microsoft Corporation Web page. This program code is all you need to open an HTML document in your browser.

Running the WebPage Macro

  1. Click the View Microsoft Word button to return to Word's Normal view.
  2. Press Alt+F8 to open the Macros dialog box, and then double-click the WebPage macro. Word runs the macro, starts Internet Explorer, and displays the Microsoft Corporation Web page in your browser, as shown in Figure 41-3. If you are asked to supply any connection information to log on to the Internet, please do so.

    click to view at full size.

    Figure 41-3. Using the Internet Explorer object model, you can quickly open your favorite Web site from within a macro.

  3. Explore the Microsoft Web site a little, and then close Internet Explorer when you're finished.

Congratulations! You have learned how to use several Office applications remotely in an Office macro. Take this accomplishment as your starting point, and continue learning about the object models for each Office application using the Object Browser tool. The wide world of macro programming awaits you!



Running Microsoft Office 2000 Small Business
Running Microsoft Office 2000
ISBN: 1572319585
EAN: 2147483647
Year: 2005
Pages: 228

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net