• Request for code critique (VBA/Excel/2003 SP3)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Request for code critique (VBA/Excel/2003 SP3)

    Author
    Topic
    #457360

    The following code was originally running without lines 40 through 80 and frequently threw an unknown error at line 30. Moments later the code would continue successfully when run in the Debug mode. Lines 40 through 80 were added and appear to have resolved the problem however I am concerned that I may be approaching the problem from the wrong direction. Any comments will be most welcome.


    Function GetQueryData(strStartURL As String, Abort As Boolean, _
    Optional strEndPage As String = vbNullString) As String
    ' Jefferson F. Scher 05/04/2006
    ' Uses IE DOM to open web page and retrieve the contents _
    of one HTML table cell into a string

    ' Requires adding the following under Tools>References:
    ' Microsoft HTML Object Library
    ' Microsoft Internet Controls

    Dim PostURL As String
    Dim MainPost As String
    Dim Board As String
    Dim ArrCol As Long
    Dim ArrRow As Long

    10 GetQueryData = "Complete"

    'Create browser object references and open an IE window
    Dim ieNew As New InternetExplorer

    LoadPage:
    20 On Error Resume Next
    30 With ieNew
    40 If Err Then
    50 On Error GoTo 0
    60 Sleep 500
    70 GoTo LoadPage
    80 End If
    90 .Visible = True 'show window
    100 .navigate strStartURL 'open page
    110 While Not .readyState = READYSTATE_COMPLETE
    120 Sleep 500 'wait 1/2 sec before trying again
    130 Wend

    140 If strEndPage vbNullString Then 'check for ending page
    Dim intQueryPos As Integer, strCurrentPage As String
    150 Do
    160 intQueryPos = InStr(1, .LocationURL, "?")
    170 If intQueryPos > 0 Then
    180 strCurrentPage = Left(.LocationURL, intQueryPos - 1)
    190 If StrComp(strCurrentPage, strEndPage, vbTextCompare) = 0 Then Exit Do
    200 End If
    210 Sleep 500 'wait 1/2 sec before trying again
    220 Loop
    ' Now wait for page to finish loading
    230 While Not .readyState = READYSTATE_COMPLETE
    240 Sleep 500 'wait 1/2 sec before trying again
    250 Wend
    260 End If
    270 End With

    Viewing 2 reply threads
    Author
    Replies
    • #1146501

      I don’t think it’s possible to have an error on “With ieNew”. What code was on line 30 before?

      • #1146692

        Hi Jefferson [indent]


        I don’t think it’s possible to have an error on “With ieNew”


        [/indent] I wouldn’t have thought so either, but that was the highlighted line on Debug.

        Line 30 was unchanged except for the addition of numbering from the source which I picked up here.

        • #1146772

          If the code compiles correctly, then you must have added a reference to Microsoft Internet Controls (Tools>References). Next time you debug, can you open the locals window and see whether ieNew was created?

          • #1146850

            What an imperfect world we live in! Using the original code; everything works again as it should. Thanks to all for the interest.

    • #1146511

      I would suggest replacing:
      Dim ieNew As New InternetExplorer
      with:

      Dim ieNew As InternetExplorer
      Set ieNew = New InternetExplorer

    • #1146514

      Don

      This article may be of some help: VB6: Taming the Wild New IE Object.

      Alan

    Viewing 2 reply threads
    Reply To: Request for code critique (VBA/Excel/2003 SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: