VB Operation IE Browser

Source: Internet
Author: User
Tags chr

I believe that the majority of computers are now using IE browser. How to control the operation of IE by programming, this article will step by step to introduce how to use VB
Call IE's object library to control ie.
The programming tools used in this article are VB6 English Enterprise Edition, the browser is IE5, the following code can only be run under IE4 and above version. In addition, in order to deep
In understanding the program code, it is best to install MSDN. All of the objects, functions, and events in this article can be found here.

How to get all the open browser windows in Windows
First open VB, create a new project, click menu Projects | References item, select in the Available References list
The Microsoft Internet controls item involves an Internet object reference into the project. Add a ListBox to Form1, and then in Form1
Add the following code:
Dim Dwinfolder as New shellwindows

Private Sub Form_Load ()
Dim objIE as Object

For each objIE in Dwinfolder
If InStr (1, Objie.fullname, "IEXPLORE. EXE ", vbTextCompare) <> 0 Then
List1.AddItem Objie.locationurl
End If
Next
End Sub
Open a few browser windows, and then run the program, and you can see that all the URL addresses in the Browse window are listed in List1.
The Shellwindows object in the above program is an object that describes the collection of all currently open Browse window objects, using the for ... Each statement can
Gets all of the Browse window objects. This is a WebBrowser object that can be found in the MSDN index through "WebBrowser object".
Detailed description of the object
The Browse Window object in Shellwindows contains not only IE, but also the Explore window (to the Explorer or My Computer window.
Explore). Only their host program is IEXPLORE.EXE, one is EXPLORE.EXE. So for each object first according to its fulname
property to determine if the window is IE browser, and if so, the URL address of the window is listed.
Many of the properties in the browser window can be obtained and set through the WebBrowser object, such as window size, toolbar, status bar status, and control
Window browsing and so on, you can find it through MSDN.

Ii. How to get the contents of each browsing window
Add a TextBox control in the Form1 of the above program, set the Multiline property to True, and then add the List1 click event as
The following code:
Dim Objdoc as Object
Dim objIE as Object

For each objIE in Dwinfolder
If Objie.locationurl = list1.list (List1.listindex) Then
Set objdoc = objie.document

For i = 1 to Objdoc.all.length-1
If Objdoc.all (i). tagname = "BODY" Then
Text1.Text = Objdoc.all (i). InnerText
End If
Next
Exit for
End If
Next
Run the program, click a list in the ListBox, and the text content in the corresponding browser window will appear in the TextBox.
In the above program, the Webrowser object is obtained first based on the URL selected in the ListBox, and then the document object is obtained based on the Documents property. We know
A page includes the head, TITLE, body section, which may also include applets, Script, joins, forms, and so on, which are an object in the document
In the program is all the objects under the Loop Document object, if the object name is "Body", the object represents the body part of the HTML document, then the object is accessed
The InnerText property gets the document body. For more detailed information about the Document object, you can refer to the WebBrowser object Help in MSDN
The document property is connected.


Iii. Responding to IE events
The above section just describes how to access the properties of the WebBrowser object, and the following describes how to monitor IE events.
Add another CommandButton in the Form1. In the Code window, add the following definition to [gengeral]-[declaration]:
Dim WithEvents Eventie as Webbrowser_v1
This adds a new object to the Form1 and then adds the following code to the Command1 click event:
Dim objIE as Object

For each objIE in Dwinfolder
If Objie.locationurl = list1.list (List1.listindex) Then
Set Eventie = objIE
command1.enabled = False
Text1.Text = ""
Exit for
End If
Next
Add the following code to the Eventie Navigatecomplete event:
Text1.Text = text1.text + chr + CHR + URL
Add the following line of code to the Unload event of the form:
Set Dwinfolder = Nothing
Run the program, select a URL in the listbox, click Command1, and then go to the Browse window with the selected option to enter the site address to browse, you can
See the addresses of the sites you have browsed are listed in the TextBox.
The above program is also very simple to implement. First, define a WebBrowser object that responds to the event, and then associate it with the WebBrowser selected in the listbox
As the browser changes, the Eventie object responds to the corresponding event.
Depending on the version of IE, the WebBrowser object is also different, the author's browser is IE5.0, on the above will eventie defined as WEBBROWSER_V1 run through, such as
If your browser version is 4.0, you may have a Type mistake error when you execute set eventie = objIE, and you can try to
Eventie defined as: Dim WithEvents Eventie as WebBrowser

The above content is not original

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.