Scripting Example: Programming Internet Explorer


To give you a taste of the power and flexibility of Automation programming, this section shows you how to program a specific Automation server: Internet Explorer. You’ll see that your scripts can control just about everything associated with Internet Explorer:

  • The position and dimensions of the window.

  • Whether or not the menu bar, toolbar, and status bar are displayed.

  • The current URL.

  • Sending the browser backward and forward between navigated URLs.

Displaying a Web Page

To get started, we’ll show you how to use the InternetExplorer object to display a specified URL. You use the Navigate method to do this, and this method uses the following syntax:

 InternetExplorer.Navigate URL, [Flags,] [TargetFrameName,] [PostData,] [Headers] 

 InternetExplorer 

A reference to the InternetExplorer object with which you’re working.

 URL 

The address of the Web page you want to display.

 Flags 

One of (or the sum of two or more of) the following integers that control various aspects of the navigation:

 

1Opens the URL in a new window.

 

2Prevents the URL from being added to the history list.

 

4Prevents the browser from reading the page from the disk cache.

 

8Prevents the URL from being added to the disk cache.

 TargetFrameName 

The name of the frame in which to display the URL. (You won’t ever need to use this parameter when scripting Internet Explorer.)

 PostData 

Specifies additional POST information that HTTP requires to resolve the hyperlink. The most common uses for this argument are to send a Web server the contents of a form, the coordinates of an image map, or a search parameter for an ASP file. If you leave this argument blank, this method issues a GET call.

 Headers 

Specifies header data for the HTTP header.

Here’s an example:

Set objIE = CreateObject(“InternetExplorer.Application”) objIE.Navigate “http://www.microsoft.com/”

Navigating Pages

Displaying a specified Web page isn’t the only thing the InternetExplorer object can do. It also has quite a few methods that give you the ability to navigate backwards and forwards through visited Web pages, refresh the current page, stop the current download, and more. Here’s a summary of these methods:

  • GoBack Navigates backward to a previously visited page.

  • GoForward Navigates forward to a previously visited page.

  • GoHome Navigates to Internet Explorer’s default Home page.

  • GoSearch Navigates to Internet Explorer’s default Search page.

  • Refresh Refreshes the current page.

  • Refresh2 Refreshes the current page using the following syntax:

    Refresh2(Level)

    Level

    A constant that determines how the page is refreshed:

     

    0Refreshes the page with a cached copy.

     

    1Refreshes the page with a cached copy only if the page has expired.

     

    3Performs a full refresh (doesn’t use a cached copy).

  • Stop Cancels the current download or shuts down dynamic page objects, such as background sounds and animations.

Using the InternetExplorer Object’s Properties

Here’s a summary of many of the properties associated with the InternetExplorer object:

  • Busy Returns True if the InternetExplorer object is in the process of downloading text or graphics. This property returns False when the complete document has been downloaded.

  • FullScreen A Boolean value that toggles Internet Explorer between the normal window and a full-screen window in which the title bar, menu bar, toolbar, and status bar are hidden.

  • Height Returns or sets the window height.

  • Left Returns or sets the position of the left edge of the window.

  • LocationName Returns the title of the current document.

  • LocationURL Returns the URL of the current document.

  • MenuBar A Boolean value that toggles the menu bar on and off.

  • StatusBar A Boolean value that toggles the status bar on and off.

  • StatusText Returns or sets the status bar text.

  • ToolBar A Boolean value that toggles the toolbar on and off.

  • Top Returns or sets the position of the top edge of the window.

  • Type Returns the type of document currently loaded in the browser.

  • Visible A Boolean value that toggles the object between hidden and visible.

  • Width Returns or sets the window width.

Running Through an Example Script

To put some of the properties and methods into practice, here’s an example script:

Option Explicit Dim objIE, objWshShell, strMessage, intResult ‘ Set up the Automation objects Set objIE = WScript.CreateObject("InternetExplorer.Application") Set objWshShell = WScript.CreateObject("WScript.Shell") ‘ Navigate to a page and customize the browser window objIE.Navigate "http://www.wordspy.com/" objIE.Toolbar = False objIE.StatusBar = False objIE.MenuBar = False ‘ Twiddle thumbs while the page loads Do While objIE.Busy Loop ‘ Get the page info strMessage = "Current URL:  " & objIE.LocationURL & vbCrLf & _     "Current Title: " & objIE.LocationName & vbCrLf & _     "Document Type: " & objIE.Type & vbCrLf & vbCrLf & _     "Would you like to view this document?" ‘ Display the info intResult = objWshShell.Popup(strMessage, , _ "Scripting IE", vbYesNo +  vbQuestion) ‘ Check the result If intResult = vbYes Then     ’ If Yes, make browser visible     objIE.Visible = True Else     ’ If no, bail out     objIE.Quit End If Set objIE = Nothing Set objWshShell = Nothing

The script begins by creating instances of the InternetExplorer and WScript Shell objects. The Navigate method displays a page, and then the toolbar, status bar, and menu bar are turned off. A Do...Loop checks the Busy property and loops while it’s True. In other words, this loop won’t exit until the page is fully loaded. Then a string variable is used to store the URL, title, and type of the page, and this string is then displayed in a Popup box, which also asks if the user wants to see the page. If the user clicks Yes, the browser is made visible; if the user clicks No, the Quit method shuts down the browser.




Insider Power Techniques for Microsoft Windows XP
Insider Power Techniques for Microsoft Windows XP (Bpg-Other)
ISBN: 0735618968
EAN: 2147483647
Year: 2005
Pages: 126

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