Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object variable or With block variable not set

Status
Not open for further replies.

patriciaxxx

Programmer
Jan 30, 2012
277
GB
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?

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]
 
Try looking at the Call Stack when the error occurs to see if that gives you a clue.
 
Hi strongm

I did try that but it didn’t seem to help me. I also read several articles that seemed to suggest it might be something to with early binding, but that didn’t help me either.

I noticed that when I comment out the following line in ‘Sub myMain()’ that the error does not occur, but that also doesn’t help as it leaves iexplorer.exe processes running?

[highlight #FCE94F]agent1.terminateIE[/highlight]

Any help would be much appreciated as it has me completely puzzled.
 
Well, the call stack should have helped you, since it should have clued you in to the fact that there was a class termination event going on, and yet you never explicitly terminate the class ...

Without going in to the details, simply replace your

agent1.terminateIE

in Sub myMain with

Set agent1 = Nothing

That should fix the issue.
 
Hi strongm

Following your previous post I did some more googling which led me to try your very suggestion of ’Set agent1 = Nothing’ and yes it has solved the problem. So thank you very much for your help with this.

One final thought, is there a way to enter form details ie userid and password using MSXML2.xmlHttp Sort of combining the following bits of code. The highlighted bit doesn’t work and I’m not sure how to code this so that it works?

Code:
[COLOR=#204A87]With CreateObject("MSXML2.xmlHttp")
    .Open "GET", "[URL unfurl="true"]http://my.ebay.co.uk/ws/eBayISAPI.dll?MyEbay&gbh=1",[/URL] False
    .send [highlight #FCE94F]"userid=123&pass=456"[/highlight]
End With

With .Document.Forms(1)
    .all("userid").Value = "User Name Here"
    .all("pass").Value = "Password Here"
End With
[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top