Use VB to implement the call and control of IE
With the continuous expansion of network applications, there is more and more documents written in HTML language, how to implement the browsing of HTML documents in the VB program is a problem that needs to be resolved. This article illustrates a method of calling and controlling Internet Explorer in VB with an example.
First, programming preparation
VB creates and controls an instance of the Internet Explorer object through OLE automation technology. Therefore, in the new project, you need to add a reference to Microsoft Internet Control. The method is to open the Reference ... option in the Visual Basic's Project drop-down menu, click the Browse ... button, Locate ShDocvw.dll under Windows's System Directory and select it, it is the Internet Explorer class library.
Second, implementation
Add the controls shown in Table 1 in Form1.
Control Type Control Name Control Content Label Lable1 Address: Text Text1 (Space) CommandCmdView Browse Command CmdForward Forward CommandCmdbackWard Back COMMANDCMDCLOSEVIEW Close Browse Window CommandCmdexit Exit
Add the following code to the following code to implement call and control of Internet Explorer,
DIM WITHEVENTS IEVIEW AS INTERNETEXPLORERERERER
'Define Internet Explorer objects in universal modules
Private sub flow_load ()
cmdforward.visible = false
cmdbackward.visible = false
cmdcloseview.visible = false
Hide the Internet Explorer control button when the form is transferred
End Sub
Private subdview_click ()
DIM TXTADDRESS AS STRING
ON Error ResMe next 'Setup Error Traps
Set ieview = getObject ("", "internetExplorer.Application")
Ieview.visible = TRUE
'Creating an instance of the Internet Explorer object and is set to be visible
TXTADDRESS = TRIM (Text1.Text)
ieview.navigate txtaddress, navnoreadfromcache
'Browse according to the contents of the Text1 control
cmdview.enabled = false
cmdexit.enabled = false
cmdforward.visible = true
cmdbackward.visible = true
cmdcloseView.visible = TRUE
'Make the browse, exit command button is invalid, so that the IE control command button is visible
End Sub
Private subduDforward_click ()
ON Error ResMe next 'Setup Error Traps
Ieview.goforward
End Sub
Private subcmdbackward_click ()
ON Error ResMe next 'Setup Error Traps
Ieview.goback
End Sub
Private subdcloseView_click ()
ON Error ResMe next 'Setup Error Traps
Ieview.quit
End Sub
Private sub ieview_onquit ()
SET IEView = Nothing
'Eliminate IEVIEW Objects
cmdforward.visible = falsecmdbackward.visible = false
cmdcloseview.visible = false
cmdview.enabled = true
cmdexit.enabled = true
'Restore the initial button status
End Sub
Private subdexit_click ()
End
End Sub
After the program starts, "Turn back", "Close", "Close Browse Window", etc., enter the corresponding URL address in the text box, click the "Browse" button to transfer the Internet Explorer to browse, this When "forward", "back", "Close the Browse Window" command button to enable visible, so that the browser is controlled, click the Close Browse window command button to close the currently open Internet Explorer window, this When the window is restored to the initial model, enter the new address again to browse.
Third, pay attention to the problem
1. The Internet Explorer object must be defined in the top layer universal module and define it as a WitHevents type to respond to events triggered by the Active X component;
2. When you browse, use the following statement to create an Internet Explorer object instance and set it to you:
Set ieview = getObject ("", "internetExplorer.Application")
Ieview.visible = TRUE
The object must be eliminated to release memory, ie
SET IEView = Nothing
Otherwise, the system is unstable due to a large number of occupation system resources.
3. Call and control the Internet Explorer process will generate various errors. If the browser history is empty, perform "forward", "back" operation generates an OLE automation error, and must be "browse", " Forward "," back "," Close ", etc.", "Close Browse Window", etc., the corresponding program module begins to add an error capture statement:
ON Error ResMe next
The above program is debugged in Windows98, VB 5.0 environments with detection and processing.