I received an interesting PM about the code I've posted to this thread basically asking:
- "What happens if I bungle this up with a typo? As is, I am prompted with a error msgbox and the querying stops…"
And this is true, written as is. The PM then had the audacity to explain to me that all I need to do is test for an error, to which I says "Er, no, don’t do that…"
What you want to do, in this case, is test the document's text for the string 'No horse was found with the specified name', which is returned on bunk queries. So I'd rewrite the code as such:<font face=Courier New><SPAN style="color:#007F00">'-----------------------------------------------------------------------</SPAN><SPAN style="color:#00007F">Private</SPAN><SPAN style="color:#00007F">Sub</SPAN> horseWebQuery()<SPAN style="color:#00007F">Dim</SPAN> ie<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Object</SPAN>, Table<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Object</SPAN><SPAN style="color:#00007F">Dim</SPAN> tblRow<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Object</SPAN>, tblCell<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Object</SPAN><SPAN style="color:#00007F">Dim</SPAN> strArr()<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, i<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN>, j<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN>, f<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN><SPAN style="color:#00007F">Dim</SPAN> tmpArr()<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Variant</SPAN>, tmpColl<SPAN style="color:#00007F">As</SPAN> Collection<SPAN style="color:#00007F">Dim</SPAN> qrySht<SPAN style="color:#00007F">As</SPAN> Worksheet<SPAN style="color:#00007F">If</SPAN><SPAN style="color:#00007F">Not</SPAN><SPAN style="color:#00007F">CBool</SPAN>(Len(ThisWorkbook.Worksheets( _
Sheet1.Index).Range("a2").Value))<SPAN style="color:#00007F">Then</SPAN><SPAN style="color:#00007F">Exit</SPAN><SPAN style="color:#00007F">Sub</SPAN><SPAN style="color:#00007F">With</SPAN> Application
.StatusBar = "Please Wait: Retrieving Web Query Results..."
.ScreenUpdating =<SPAN style="color:#00007F">False</SPAN>
.DisplayAlerts =<SPAN style="color:#00007F">False</SPAN><SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN><SPAN style="color:#00007F">On</SPAN><SPAN style="color:#00007F">Error</SPAN><SPAN style="color:#00007F">Resume</SPAN><SPAN style="color:#00007F">Next</SPAN>
ThisWorkbook.Worksheets("Horse Errors").Delete<SPAN style="color:#00007F">On</SPAN><SPAN style="color:#00007F">Error</SPAN><SPAN style="color:#00007F">GoTo</SPAN> 0<SPAN style="color:#00007F">Set</SPAN> qrySht = ThisWorkbook.Worksheets(Sheet1.Index)<SPAN style="color:#00007F">With</SPAN> qrySht
<SPAN style="color:#00007F">Let</SPAN> tmpArr = .Range(.Range("a2"), _
.Range("A65536").End(xlUp).Item(2)).Value
.Range("B1:F65536").ClearContents
.Range("B1:F1").Value = Array( _
"Horse Name", "Born", "Sex", "Sire", "Dam")<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN><SPAN style="color:#00007F">On</SPAN> Error<SPAN style="color:#00007F">GoTo</SPAN> errHandler<SPAN style="color:#00007F">Set</SPAN> ie = CreateObject("InternetExplorer.Application")
ie.navigate "http://www.drf.com/workoutHorseSearch.do"<SPAN style="color:#00007F">Do</SPAN><SPAN style="color:#00007F">While</SPAN> ie.busy: DoEvents:<SPAN style="color:#00007F">Loop</SPAN><SPAN style="color:#00007F">Do</SPAN><SPAN style="color:#00007F">While</SPAN> ie.ReadyState<> 4: DoEvents:<SPAN style="color:#00007F">Loop</SPAN><SPAN style="color:#00007F">Set</SPAN> tmpColl =<SPAN style="color:#00007F">New</SPAN> Collection<SPAN style="color:#00007F">For</SPAN> f =<SPAN style="color:#00007F">LBound</SPAN>(tmpArr, 1)<SPAN style="color:#00007F">To</SPAN><SPAN style="color:#00007F">UBound</SPAN>(tmpArr, 1)
<SPAN style="color:#00007F">If</SPAN><SPAN style="color:#00007F">CBool</SPAN>(Len(tmpArr(f, 1)))<SPAN style="color:#00007F">Then</SPAN>
<SPAN style="color:#00007F">Let</SPAN> i = 0: <SPAN style="color:#00007F">Let</SPAN> j = 0
<SPAN style="color:#00007F">With</SPAN> ie
.document.Forms("HorseSearchForm").Name.Value = tmpArr(f, 1)
.navigate "JavaScript:if (fnValidate()) document.HorseSearch<SPAN style="color:#00007F">For</SPAN>m.submit();"
<SPAN style="color:#00007F">Do</SPAN><SPAN style="color:#00007F">While</SPAN> .busy: DoEvents:<SPAN style="color:#00007F">Loop</SPAN>
<SPAN style="color:#00007F">Do</SPAN><SPAN style="color:#00007F">While</SPAN> .ReadyState<> 4: DoEvents:<SPAN style="color:#00007F">Loop</SPAN>
<SPAN style="color:#00007F">If</SPAN><SPAN style="color:#00007F">Not</SPAN><SPAN style="color:#00007F">CBool</SPAN>(InStrB(1, .document.body.innerText, _
"No horse was found with the specified name"))<SPAN style="color:#00007F">Then</SPAN>
<SPAN style="color:#00007F">Set</SPAN> Table = .document.all.tags("table").Item(13)
<SPAN style="color:#00007F">ReDim</SPAN> strArr(1<SPAN style="color:#00007F">To</SPAN> Table.Rows.Length - 2, 1<SPAN style="color:#00007F">To</SPAN> 5)
<SPAN style="color:#00007F">For</SPAN><SPAN style="color:#00007F">Each</SPAN> tblRow<SPAN style="color:#00007F">In</SPAN> Table.Rows
<SPAN style="color:#00007F">Let</SPAN> j = 1: <SPAN style="color:#00007F">Let</SPAN> i = i + 1
<SPAN style="color:#00007F">If</SPAN> i > 2<SPAN style="color:#00007F">Then</SPAN>
<SPAN style="color:#00007F">For</SPAN><SPAN style="color:#00007F">Each</SPAN> tblCell<SPAN style="color:#00007F">In</SPAN> tblRow.Cells
strArr(i - 2, j) = tblCell.innerText
<SPAN style="color:#00007F">Let</SPAN> j = j + 1
<SPAN style="color:#00007F">Next</SPAN> tblCell
<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">Next</SPAN> tblRow
qrySht.Range("b65536").End(xlUp).Item( _
2, 1).Resize(UBound(strArr, 1), 5).Value = strArr
Erase strArr
Else: tmpColl.Add "A" & f + 1 & ": " & tmpArr(f, 1)
<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><SPAN style="color:#00007F">Next</SPAN><SPAN style="color:#00007F">With</SPAN> qrySht
.Columns("B:F").AutoFit
.Columns("C:C").TextToColumns Destination:=.Range("C1"), _
DataType:=xlDelimited<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN><SPAN style="color:#00007F">If</SPAN><SPAN style="color:#00007F">CBool</SPAN>(tmpColl.Count)<SPAN style="color:#00007F">Then</SPAN>
<SPAN style="color:#00007F">ReDim</SPAN> strArr(1<SPAN style="color:#00007F">To</SPAN> tmpColl.Count, 1<SPAN style="color:#00007F">To</SPAN> 1)
ThisWorkbook.Worksheets.Add(after:=qrySht).Name = _
"Horse Errors"
For i = 1<SPAN style="color:#00007F">To</SPAN> tmpColl.Count
strArr(i, 1) = tmpColl(i)
<SPAN style="color:#00007F">Next</SPAN>
<SPAN style="color:#00007F">With</SPAN> ThisWorkbook.Worksheets("Horse Errors")
.Range("A2").Resize(UBound(strArr, 1)).Value = strArr
<SPAN style="color:#00007F">With</SPAN> .Range("A1")
.Value = "Query Errors"
.Font.Bold =<SPAN style="color:#00007F">True</SPAN>
.Font.Underline = xlUnderlineStyleSingle
.HorizontalAlignment = xlCenter
.EntireColumn.AutoFit
<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN>
Application.Goto qrySht.Range("a1")<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN>
errHandler:
ie.Quit:<SPAN style="color:#00007F">Set</SPAN> ie =<SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">Set</SPAN> qrySht =<SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">Set</SPAN> tmpColl =<SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">With</SPAN> Application
.StatusBar =<SPAN style="color:#00007F">False</SPAN>
.ScreenUpdating =<SPAN style="color:#00007F">True</SPAN>
.DisplayAlerts =<SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">If</SPAN> Err.Number<> 0<SPAN style="color:#00007F">Then</SPAN>
MsgBox "Error: " & String$(2, vbLf) & _
Err.Number & String$(2, vbLf) & _
Err.Description
Err.Clear
<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Sub</SPAN><SPAN style="color:#007F00">'-----------------------------------------------------------------------</SPAN></FONT>
And, now the following is the query results Worksheet, with the query names in Column A:
HorseWebQuery2.xls |
---|
|
---|
| A | B | C | D | E | F |
---|
1 | Horse Names | Horse Name | Born | Sex | Sire | Dam |
---|
2 | Folklore | Folklore | | M | Folgsam | Fokkela |
---|
3 | Foobar | Folklore | 3/20/1993 | M | Native Prospector | Fantazia |
---|
4 | Point Given | Folklore (GB) | 2/5/1995 | M | Fairy King | Falsoola (GB) |
---|
5 | Bungle in the Jungle | Folklore (JPN) | 3/8/1999 | M | Dance in the Dark (JPN) | Andes Lady |
---|
6 | Sensation | Folklore | 2/26/2003 | F | Tiznow | Contrive |
---|
7 | | Point Given | 4/26/2000 | G | Apollo | Kenney Lady |
---|
8 | | Point Given | 3/27/1998 | H | Thunder Gulch | Turko's Turn |
---|
9 | | Sensation (GB) | 4/3/1993 | M | Soviet Star | Outstandingly |
---|
10 | | Sensation | 5/9/1994 | G | Key to the Mint | Azuma Brook (JPN) |
---|
11 | | Sensation (ITY) | 1/4/2002 | F | Late Parade (IRE) | Padme |
---|
12 | | Sensation | 1/24/2003 | F | Dixie Union | Ryn |
---|
|
---|
And, because cells A3 and A5 have invalid queries, a new error worksheet 'Horse Errors' is created, and looks like:
HorseWebQuery2.xls |
---|
|
---|
| A | B | C | D |
---|
1 | Query Errors | | | |
---|
2 | A3: Foobar | | | |
---|
3 | A5: Bungle in the Jungle | | | |
---|
|
---|
I hope this helps.