patriciaxxx
Programmer
I have the following Module which retrieves data from myebay page (if I’m already logged in) and Class Module named ‘agent’.
All code compiles successfully.
When I run ‘Sub myMain()’ for the first time everything works ok.
But if I run it a second time it raises Run time error 91 in the Class Module on the line I have highlighted in yellow.
Can anyone help me work out what’s wrong?
All code compiles successfully.
When I run ‘Sub myMain()’ for the first time everything works ok.
But if I run it a second time it raises Run time error 91 in the Class Module on the line I have highlighted in yellow.
Can anyone help me work out what’s wrong?
Code:
[COLOR=#204A87]Option Compare Database
Option Explicit
Public agent1 As agent
Public theLink As String
Public theTitle As String
Public thePrice As String
Public Sold As Boolean
Public strSold As String
Public shipping As String
Public theDate As String
Sub myMain()
Dim text As String
Set agent1 = New agent
agent1.visible = True
agent1.openpage "[URL unfurl="true"]http://my.ebay.co.uk/ws/eBayISAPI.dll?MyEbay&gbh=1"[/URL]
agent1.text = agent1.explorer.Document.all(1).outerhtml
agent1.position = 1
agent1.visible = False
Do While getEbayData
Debug.Print theLink
Loop
agent1.terminateIE
End Sub
Function getEbayData() As Boolean
If agent1.moveTo("g-asm") Then
agent1.moveTo ("href=""")
theLink = agent1.getText("""")
Else
getEbayData = False
Exit Function
End If
agent1.moveTo (""">")
theTitle = agent1.getText("<")
agent1.moveTo ("prices")
agent1.moveTo (">")
thePrice = agent1.getText(">")
If InStr(1, thePrice, "binsold") Then
thePrice = extractValue(agent1.getText("<"))
Sold = False
strSold = "Not Sold"
Else
thePrice = extractValue(agent1.getText("<"))
Sold = True
strSold = "Sold"
End If
agent1.moveTo ("<SPAN class=")
shipping = agent1.getText(">")
If InStr(1, shipping, "fee") Then
shipping = extractValue(agent1.getText("<"))
Else
shipping = "0"
End If
agent1.moveTo ("time time rt"">")
theDate = agent1.getText("<")
getEbayData = True
End Function
Function extractValue(text As String) As Double
Dim theNumber As String
Dim cChar As String
Dim i As Integer
For i = 1 To Len(text)
cChar = Mid(text, i, 1)
If IsNumeric(cChar) Or cChar = "." Then
theNumber = theNumber & cChar
End If
Next i
If theNumber <> "" Then
extractValue = CDbl(theNumber)
Else
extractValue = -1
End If
End Function
Function fixEquals(text As String) As String
If Left(text, 1) = "=" Then
fixEquals = "'" & text
Else
fixEquals = text
End If
End Function
[/color]
Code:
[COLOR=#204A87]Option Compare Database
Option Explicit
Dim ie As Object
Dim theHTML As String
Dim pos As Long
Public Property Get visible() As Boolean
visible = ie.visible
End Property
Public Property Let visible(theValue As Boolean)
ie.visible = theValue
End Property
Public Sub updateHTML()
If Len(ie.Document.all(1).outerhtml) > Len(ie.Document.all(0).outerhtml) Then
theHTML = ie.Document.all(1).outerhtml
Else
theHTML = ie.Document.all(0).outerhtml
End If
End Sub
Public Property Get text() As String
text = theHTML
End Property
Public Property Let text(theValue As String)
theHTML = theValue
End Property
Public Property Get position() As Long
position = pos
End Property
Public Property Let position(theValue As Long)
pos = theValue
If pos < 1 Then pos = 1
End Property
Public Property Get explorer() As Object
Set explorer = ie
End Property
Sub initializeIE()
'Call this subprocedure to start internet explorer up.
Set ie = CreateObject("internetexplorer.application")
pos = 1
End Sub
Sub terminateIE()
'Call this subprocedure when you are finished with IE to close it down.
[highlight #FCE94F] ie.quit[/highlight]
Set ie = Nothing
End Sub
Function getElement(NameOrID As String)
Set getElement = ie.Document.getElementByID(NameOrID)
End Function
Function getText(theString As String) As String
Dim myPos As Long
myPos = InStr(pos, theHTML, theString)
If myPos = 0 Then
getText = ""
Else
getText = Mid(theHTML, pos, myPos - pos)
pos = myPos + Len(theString)
End If
End Function
Function moveTo(theString As String, Optional ByVal theCount As Integer) As Boolean
Dim x As Integer
If theCount = 0 Then theCount = 1
moveTo = True
For x = 1 To theCount
If Not singleMoveTo(theString) Then
moveTo = False
Exit Function
End If
Next
End Function
Private Function singleMoveTo(theString As String) As Boolean
Dim myPos As Long
myPos = InStr(pos, theHTML, theString)
If myPos = 0 Then
singleMoveTo = False
Else
singleMoveTo = True
pos = myPos + Len(theString)
End If
End Function
Sub openpage(url As String)
ie.navigate url
waitForLoad
updateHTML
End Sub
Sub waitForLoad()
Do Until ie.readyState = 4: DoEvents: Loop
updateHTML
End Sub
Private Sub Class_Initialize()
initializeIE
End Sub
Private Sub Class_Terminate()
terminateIE
End Sub
[/color]