Sie sind auf Seite 1von 3

06/07/2015

AutomateInternetExplorerJPSoftwareTechnologies

Search this website

SEARCH

Automate Internet Explorer


Here I will demonstrate some example code for automating Internet Explorer through VBA. You can open web pages, click
buttons, import data, etc. The possibilities are endless.
These examples use late binding with the following two object libraries: Microsoft Internet Controls (shdocvw.dll) and
Microsoft HTML Object Library (MSHTML.TLB).
There are also some samples for getting data from the web using the XMLHTTP object through different web APIs on my blog.

Open a webpage and display (simple)


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

SubGoToWebSite()
DimappIEAsObject'InternetExplorer
DimsURLAsString
Application.ScreenUpdating=False

SetappIE=GetIE

sURL="http://www.jpsoftwaretech.com"

WithappIE
.NavigatesURL
.Visible=True
EndWith

Application.ScreenUpdating=True

SetappIE=Nothing
EndSub

Open a webpage, fill in form fields and click buttons


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

SubGoToWebSiteAndPlayAround()

DimappIEAsObject'InternetExplorer.Application
DimsURLAsString
DimUserNAsObject'MSHTML.IHTMLElement
DimPWAsObject'MSHTML.IHTMLElement
DimElementAsObject'HTMLButtonElement
DimbtnInputAsObject'MSHTML.HTMLInputElement
DimElementColAsObject'MSHTML.IHTMLElementCollection
DimLinkAsObject'MSHTML.HTMLAnchorElement
DimstrCountBodyAsString
DimlStartPosAsLong
DimlEndPosAsLong
DimTextIWantAsString

Application.ScreenUpdating=False

SetappIE=GetIE

sURL="http://www.jpsoftwaretech.com"

WithappIE
.NavigatesURL
'uncommentthelinebelowifyouwanttowatchthecodeexecute,orfordebugging
'.Visible=True
EndWith

'loopuntilthepagefinishesloading
DoWhileappIE.Busy
Loop

'enterusernameandpasswordintextboxes
SetUserN=appIE.Document.getElementsByName("username")
IfNotUserNIsNothingThen
'fillinfirstelementnamed"username",assumedtobetheloginnamefield
UserN(0).Value="loginname"
EndIf

SetPW=appIE.Document.getElementsByName("password")

http://www.jpsoftwaretech.com/excelvba/automateinternetexplorer/

1/3

06/07/2015
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

AutomateInternetExplorerJPSoftwareTechnologies
'password
IfNotPWIsNothingThen
'fillinfirstelementnamed"password",assumedtobethepasswordfield
PW(0).Value="myPassword"
EndIf

'click'Submit'button
SetElementCol=appIE.Document.getElementsByTagName("INPUT")

'loopthroughall'input'elementsandfindtheonewiththevalue"Submit"
ForEachbtnInputInElementCol
IfbtnInput.Value="Submit"Then
btnInput.Click
ExitFor
EndIf
NextbtnInput

'loopuntilthepagefinishesloading
DoWhileappIE.Busy
Loop

'clickabuttononthenextpage
SetElementCol=appIE.Document.getElementsByTagName("INPUT")

'loopthroughall'input'elementsandfindtheonewithaspecificvalue
ForEachbtnInputInElementCol
IfbtnInput.Value="LinkPage#1"Then
btnInput.Click
ExitFor
EndIf
NextbtnInput

'loopuntilthepagefinishesloading
DoWhileappIE.Busy
Loop

'clickatextlinkonthepageafterthat
SetElementCol=appIE.Document.getElementsByTagName("a")

ForEachLinkInElementCol
IfLink.innerHTML=<strong>ClickableTextLinkName</strong>"Then
Link.Click
ExitFor
EndIf
NextLink

'loopuntilthepagefinishesloading
DoWhileappIE.Busy
DoEvents
Loop

'grabsometextfromthebody
strCountBody=appIE.Document.body.innerText
lStartPos=InStr(1,strCountBody,"Texttofind")
lEndPos=lStartPos+12
TextIWant=Mid$(strCountBody,lStartPos,lEndPoslStartPos)

'grabthewholescreen&amp;amp;amp;amp;pasteintoExcel
appIE.ExecWBOLECMDID_SELECTALL,OLECMDEXECOPT_DONTPROMPTUSER
appIE.ExecWBOLECMDID_COPY,OLECMDEXECOPT_DODEFAULT

Workbooks.Add
ActiveSheet.Paste

Application.ScreenUpdating=True
appIE.Quit
EndSub

I'll admit this isn't the best example; there's no login form on my homepage. But let's examine these samples closely anyway.
The first one simply starts an late-bound instance of Internet Explorer, assigns a url (string) to a variable and then passes it to
the Internet Explorer application variable. Then it makes the window visible, destroys the variable and exits.
The second example shows in much more detail how to manipulate Internet Explorer.
This function creates the InternetExplorer object:

Code:
1
2
3
4

FunctionGetIE()AsObject
OnErrorResumeNext
SetGetIE=CreateObject("InternetExplorer.Application")
EndFunction

Use Do While appIE.Busy: DoEvents: Loop to keep Excel waiting until the page loads completely.
Textboxes (username, password, etc) that you fill in are usually named elements on a webpage. The code uses
http://www.jpsoftwaretech.com/excelvba/automateinternetexplorer/

2/3

06/07/2015

AutomateInternetExplorerJPSoftwareTechnologies

appIE.Document.getElementsByName("username")
to find the textbox named "username" and put your chosen text there. To find the name of the text box, click on View>Source
in Internet Explorer when viewing the page. You will be using View>Source extensively to get the names of various elements
on each page you want to access.
To click buttons on a site, you can cycle through each Input Element of the Elements Collection until you find the button
named "Submit":
appIE.Document.getElementsByTagName("INPUT")
Again, you would need to View Source to see the tag name is "INPUT" (the type of element) and the name of the button is
"Submit". The Click Method actually does the clicking of the button to load the next page or input whatever you placed in the
text boxes.
To click on a link on a page, you would loop through the tags that start with 'a' (links) until you find the one with the exact
name you are looking for. You need to include everything between the <a> and </a> tags; so in the code above, the webmaster
put 'bold' tags in the link (). That would need to be included because technically it is part of the text of the link (but bad web
design if you ask me).
appIE.Document.getElementsByTagName("a")
Now to grab the body text (the real reason you are here, I'm sure), assign the innerText property to a String variable:
appIE.Document.body.innerText
In the sample above, I use the Instr property to look for some text, then the Mid function to extract it from the webpage. At
that point you could simply paste it into a workbook (see Latitude and Longitude functions for the GetDistance() UDF) but I
went on and just copied and pasted the entire displayed webpage into a new workbook.
Hopefully these samples will help you automate Internet Explorer in your own projects.

More Examples
For even more examples, check out:
An exploration of IE browser methods, part III
An exploration of IE browser methods, part II
An exploration of IE browser methods, part I
Search Engine Browsing
Screen Scraping 101 with VBA

http://www.jpsoftwaretech.com/excelvba/automateinternetexplorer/

3/3

Das könnte Ihnen auch gefallen