I am running a screen scrape in vba. When I step through the screen scrape it works fine. When I play the screen scrape, the code intermittenly scrapes the wrong set of screens. I'm stumped. I've got "While ie.Busy: DoEvents: Wend" all over the place.
Am I missing something?
Below is a sample of the code. The Sub WebPage() basically "calls" Sub GetOneTable():
Sub WePage()
LinkHref = "javascript:__doPostBack('GridView1','Select$13')": ie.navigate LinkHref
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$0')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.theerrorwebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$1')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.thewebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$2')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.theerrorwebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$3')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.theerrorwebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$4')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.theerrorwebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$5')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.theerrorwebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
End Sub
Sub GetOneTable(d, n)
' d is the document
' n is the table to extract
Dim e As Object ' the elements of the document
Dim t As Object ' the table required
Dim r As Object ' the rows of the table
Dim c As Object ' the cells of the rows
Dim I As Long
Dim J As Long
Range("A1").Select
Do Until ActiveCell.Row = 1048576
Selection.End(xlDown).Select
Loop
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
For Each e In d.all
If e.ID = "GridView1" Then
J = J + 1
End If
If J = n Then
Set t = e
tabno = tabno + 1
nextrow = nextrow + 1
Set Rng = ActiveCell
For Each r In t.Rows
For Each c In r.Cells
Rng.Value = c.innertext
Set Rng = Rng.Offset(, 1)
I = I + 1
Next c
nextrow = nextrow + 1
Set Rng = Rng.Offset(1, -I)
I = 0
Next r
Exit For
End If
Next e
End Sub
Am I missing something?
Below is a sample of the code. The Sub WebPage() basically "calls" Sub GetOneTable():
Sub WePage()
LinkHref = "javascript:__doPostBack('GridView1','Select$13')": ie.navigate LinkHref
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$0')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.theerrorwebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$1')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.thewebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$2')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.theerrorwebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$3')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.theerrorwebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$4')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.theerrorwebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
LinkHref = "javascript:__doPostBack('GridView1','Select$5')"
If LinkHref <> "" Then
ie.navigate LinkHref
End If
While ie.Busy: DoEvents: Wend
If ie.Document.URL = "https://www.theerrorwebpage.aspx" Then
While ie.Busy: DoEvents: Wend
GoTo Card27:
Else
End If
Set Doc = ie.Document
GetOneTable Doc, 1
Cells.Replace What:="Select", Replacement:="xxxxxxxxxxxxxxx", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ie.navigate "https://www.thewebpage.aspx"
While ie.Busy: DoEvents: Wend
End Sub
Sub GetOneTable(d, n)
' d is the document
' n is the table to extract
Dim e As Object ' the elements of the document
Dim t As Object ' the table required
Dim r As Object ' the rows of the table
Dim c As Object ' the cells of the rows
Dim I As Long
Dim J As Long
Range("A1").Select
Do Until ActiveCell.Row = 1048576
Selection.End(xlDown).Select
Loop
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
For Each e In d.all
If e.ID = "GridView1" Then
J = J + 1
End If
If J = n Then
Set t = e
tabno = tabno + 1
nextrow = nextrow + 1
Set Rng = ActiveCell
For Each r In t.Rows
For Each c In r.Cells
Rng.Value = c.innertext
Set Rng = Rng.Offset(, 1)
I = I + 1
Next c
nextrow = nextrow + 1
Set Rng = Rng.Offset(1, -I)
I = 0
Next r
Exit For
End If
Next e
End Sub