View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Import data from web page into excel

Create a worksheet called DEALERS and run this code to see an example of
calling an Internet explorer will do.

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



"rik" wrote:


Hi Joel,
sorry, but i cannot post the site, it's my brooker, access with password
only ...
I tested with the simpliest thing to do and that doesn't even work :
open internet explorer and go to www.google.com
select whatever part of text
open excel,write and run the following :
Sub test()
Sheets("Blad1").Select
SendKeys "a", True
DoEvents
SendKeys "{ENTER}", True
DoEvents
SendKeys "%{TAB}", True
DoEvents
SendKeys "^C", True
DoEvents
SendKeys "%{TAB}", True
DoEvents
SendKeys "^V", True
End Sub

It will block explorer and excel ...
What can i do ?