I have two examples of programs below. I use a combination of techniques to
backwards engineer Web code. I don't know Java very well and I'm not
experienced enough to be an expert but I can usally get things to work by
brute force method.
I usally got the webpage using Internet Explorer and use the menu View -
Source which will open a NotePad of the code. I look for two things
1) Tags, tags look like this
start of tag is an angle bracket with the name and the close is
an angle bracket with name and backslash
< A ..............some text ........... /A >
You can get tags in code below using this statement
Set A_Tags = IE.Document.getelementsbytagname("A")
2) I also look in the source code for ID's which are : id =abc
You can get these in code with
'Set ABC = IE.document.getElementById("abc")
The boxes you are looking for should have usique ids which will get you the
locations to put the data.
I also dump all the items to a worksheet (sheet1) like I did in the macro
GenericCode() below. There is a for loop with ITM in this code. I also put
a break point in the for loop and then add ITM to the watch window to help me
debug code. You can change the URL in this code to your website to help you
find the name of the boxes.
In the first Nissan code I added data to a text box using these two lines
Set radius = IE.document.getElementById("radius")
radius.Value = "100"
You need to do something similar in your code.
I hope this will get you started.
------------------------------------------------------------------------------------------------
Nissan Dealer code
Make a Worksheet called DEALERS and run this code
--------------------------------------------------------------------------------------------
Sub GetDealers()
'Dim PageNumber As Object
CR = Chr(13)
LF = Chr(10)
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "
http://www.nissanusa.com/apps/dealerlocator"
Request = "?zipCode=07508&tool=Home.Locator"
'get web page
IE.Navigate2 URL & Request
Do While IE.readyState <> 4
DoEvents
Loop
'get search button
Set but = IE.document.getElementById("mainSearchButton")
'put distance in listbox on webpage
Set radius = IE.document.getElementById("radius")
radius.Value = "100"
'search again a larger distance
'Select Search button and activate
but.Select
but.Click
Set SearchResults = IE.document.getElementById("searchResults")
On Error Resume Next ' Defer error handling.
Do
Err.Clear
Set PageNumber = IE.document.getElementById("pageNumber")
Pages = PageNumber.Value
DoEvents
Loop While Err.Number <> 0
On Error GoTo 0
With Sheets("Dealers")
.Cells.ClearContents
RowCount = 1
For PageCount = 1 To PageNumber.Length
PageNumber.Value = Format(PageCount, "@")
PageNumber.onchange
For Each Chld In SearchResults.Children
If Chld.innertext = "" Then
Exit For
End If
Set DealerNumberObj = _
Chld.getelementsbytagname("A")
DealerNumberStr = DealerNumberObj.Item(1).pathname
dealerNumber = _
Val(Mid(DealerNumberStr, InStr(DealerNumberStr, "'") + 1))
.Cells(RowCount, "A") = dealerNumber
ColCount = 2
dealer = Chld.innertext
Do While InStr(dealer, CR) > 0
Data = Trim(Left(dealer, InStr(dealer, CR) - 1))
'remove leading CR and LF
Do While Left(Data, 1) = LF Or _
Left(Data, 1) = CR
Data = Mid(Data, 2)
Loop
dealer = Trim(Mid(dealer, InStr(dealer, CR) + 1))
If InStr(Data, "(") > 0 And _
ColCount = 4 Then
Distance = Trim(Mid(Data, InStr(Data, "(") + 1))
Distance = Trim(Left(Distance, InStr(Distance, ")") - 1))
CityState = Trim(Left(Data, InStr(Data, "(") - 1))
.Cells(RowCount, ColCount) = CityState
.Cells(RowCount, (ColCount + 1)) = Distance
ColCount = ColCount + 2
Else
.Cells(RowCount, ColCount) = Data
ColCount = ColCount + 1
End If
Loop
'remove leading CR and LF
Do While Left(dealer, 1) = LF Or _
Left(dealer, 1) = CR
dealer = Mid(dealer, 2)
Loop
.Cells(RowCount, ColCount) = dealer
RowCount = RowCount + 1
Next Chld
Next PageCount
End With
End Sub
------------------------------------------------------------------------------------------
'Run this code in a workbook with a sheet name SHEET1
Sub GenericCode()
'Enter your URL here
URL = "
http://www.shockwave.com"
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
'get web page
IE.Navigate2 URL
Do While IE.readyState <> 4
DoEvents
Loop
'get TAG Item
Set A_Tags = IE.Document.getelementsbytagname("A")
'Set but = IE.document.getElementById("mainSearchButton")
RowCount = 1
With Sheets("Sheet1")
For Each itm In IE.Document.all
.Range("A" & RowCount) = itm.classname
.Range("B" & RowCount) = itm.tagname
' .Range("C" & RowCount) = Left(itm.innertext, 256)
.Range("D" & RowCount) = Left(itm.innerhtml, 256)
RowCount = RowCount + 1
Next itm
End With
End Sub