1

I have a webpage: https://www.brcdirectory.com/InternalSite/Site.aspx?BrcSiteCode=1832583

I want to retrieve some text from this page, from within a HTML <Span ID>.

<span id="ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate">Expiry Date : 07/12/2017</span>

I have IE 11.0.9600.18639

Via Excel, I am using the below code to open IE 11, navigate to the page and want to try and display a message box of the text inside the <SPAN>.

Code:

Option Explicit  

Sub GoToWebsiteTest()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim appIE As Object
    Dim objElement As Object
    Dim objCollection As Object
    Dim i As Long, LastRow As Long, sFolder As String
    Dim sURL As String, FILE As String

    LastRow = Range("I" & Rows.Count).End(xlUp).Row
    For i = 6 To LastRow
        Set appIE = New InternetExplorerMedium

        sURL = "https://www.brcdirectory.com/InternalSite/Site.aspx?BrcSiteCode=" & Range("I392").Value
        With appIE
            .navigate sURL
            .Visible = True
        End With

        Do While appIE.Busy Or appIE.READYSTATE <> READYSTATE_COMPLETE
            DoEvents
        Loop

        Set objCollection = appIE.document.getElementById("ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate")
        MsgBox Replace(objCollection.innerText, "Expiry Date : ", "")

        appIE.Quit
        Set appIE = Nothing
    Next i

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

    MsgBox "All BRCs Succesfully Updated."
End Sub

I have tried everything! I have tried so many variations of this line where I get the error:

Do While appIE.Busy Or appIE.READYSTATE <> READYSTATE_COMPLETE

But alas I get this annoying error:

Runtime Error: -2147467259 (80004005)
Method 'Busy' of object 'IWebBrowser2' failed.

Please, please can someone show me what i am doing wrong. This is driving me crazy. Thanks in advance.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
user7415328
  • 1,053
  • 5
  • 24
  • 61

2 Answers2

1

If you don't want to use the "get from web" you can use this code.

Sub expiry()

    Dim RE As Object
    Dim HTML As String
    Set RE = CreateObject("vbscript.regexp")
    HTML = GetHTML("https://www.brcdirectory.com/InternalSite/Site.aspx?BrcSiteCode=1832583")


    'Expiry Date : 07/12/2017
    RE.Pattern = "(Expiry Date : \d{2}\/\d{2}\/\d{4})"
    RE.Global = True
    RE.IgnoreCase = True
    Set Matches = RE.Execute(HTML)


    ExpiryDate = Matches.Item(0).submatches.Item(0)

End Sub


Function GetHTML(URL As String) As String
    Dim HTML As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .Send
        GetHTML = .ResponseText
    End With
End Function

ExpiryDate will contain the text you wanted (I think).

If you only wanted the actual date you can use RE.Pattern = "Expiry Date : (\d{2}\/\d{2}\/\d{4})"

EDIT;
In response to comments below:
This is the references I have enabled
enter image description here

EDIT based on download to textfile.

Private Declare Function URLDownloadToFileA Lib "urlmon" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long



    Sub expiry()

        Dim RE As Object
        Dim HTML As String
        Dim MyData As String

        Set RE = CreateObject("vbscript.regexp")
        DownloadFile "https://www.brcdirectory.com/InternalSite/Site.aspx?BrcSiteCode=1832583", "C:\TEST\goog.txt"


        Open "C:\TEST\goog.txt" For Binary As #1
        HTML = Space$(LOF(1))
        Get #1, , HTML
        Close #1


        'Expiry Date : 07/12/2017
        RE.Pattern = "(Expiry Date : \d{2}\/\d{2}\/\d{4})"
        RE.Global = True
        RE.IgnoreCase = True
        Set Matches = RE.Execute(HTML)


        ExpiryDate = Matches.Item(0).submatches.Item(0)

    End Sub



    Private Function DownloadFile(URL As String, LocalFilename As String) As Boolean
         'Thanks Mentalis:)
        Dim lngRetVal As Long
        lngRetVal = URLDownloadToFileA(0, URL, LocalFilename, 0, 0)
        If lngRetVal = 0 Then DownloadFile = True
    End Function

EDIT again. enter image description here

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • thanks for the code, i would rather not use get from web form to be honest. When i try to use your code i get an 'Access Denied' error on the .send component – user7415328 May 19 '17 at 08:16
  • I see the createObject has "MSXML2.XMLHTTP") do i need to add a reference in the library to XML? – user7415328 May 19 '17 at 08:17
  • Don't know... I was a bit surprised about your error. Still thinking. But you may be correct about reference – Andreas May 19 '17 at 08:19
  • @user7415328 I found this also: http://stackoverflow.com/questions/22938194/xmlhttp-request-is-raising-an-access-denied-error – Andreas May 19 '17 at 08:24
  • ok thanks for the link, i read that and tried changing 'With CreateObject("MSXML2.XMLHTTP")' to 'With CreateObject("Msxml2.ServerXMLHTTP")' and instead of 'Access Denied', now i get 'A connection with the server could not be established'. – user7415328 May 19 '17 at 08:37
  • @user7415328 That is odd. I can not find what is the problem. The only thing I can think of is if you use the code I wrote above in a new document or delete `Option Explicit` from your document. Maybe it's complaining that CreateObject is not declared in a strange way – Andreas May 19 '17 at 08:50
  • would it matter which version of IE im using? Are you using IE 11? maybe its a version compatibility thing or maybe the fact im on a work computer with a group policy. But if it lets me access the site without vba then i do not see why it would be stopping me from accessing it using vba – user7415328 May 19 '17 at 09:00
  • i tried the code without option explicit and in a new workbook but same connection error occurs – user7415328 May 19 '17 at 09:01
  • Yes IE 11.0.9600.18665 I have no clue what the issue is. I googled "vba download html source" and there are some other methods to do it. One way is to download the html as a textfile. See here example1() http://www.vbaexpress.com/forum/showthread.php?27050-Save-web-page-source-as-text-file I have not tried it but if that works we can go from there. – Andreas May 19 '17 at 09:07
  • @user7415328 I made an edit based on the download to textfile from the link above. Remember to create the folder C:\TEST\ first. – Andreas May 19 '17 at 09:25
  • thanks for the edit. I tried the code and get an invalid procedure or arguement error on this line ExpiryDate = Matches.Item(0).submatches.Item(0) – user7415328 May 19 '17 at 10:29
  • Last line of code... Bugger... Try to set a watch on Matches in VBA editor. I will update my answer with a picture of how it looks for me. Note I have ExpiryDate also in the watch above. – Andreas May 19 '17 at 10:34
  • @user7415328 Are you using the exact same code as I am? No change in the pattern? – Andreas May 19 '17 at 10:35
  • @user7415328 Does your Matches look the same as mine? – Andreas May 19 '17 at 10:42
  • @user7415328 You can try to replace that row with `ExpiryDate = Matches.Item(0)` – Andreas May 19 '17 at 10:53
1

I managed to resolve this by using the following code:

Option Explicit
Private ieBrowser As InternetExplorer

Sub GetBRCText()
    Dim i As Long, LastRow As Long
    Dim a As Range, b As Range
    Dim strDocHTML As String, strDocHTML2 As String
    Dim dteStartTime As Date

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    On Error Resume Next

    LastRow = ThisWorkbook.ActiveSheet.Range("I" & Rows.Count).End(xlUp).Row
    Set a = Range("I6:I" & LastRow)

    'Create a browser object
    Set ieBrowser = CreateObject("internetexplorer.application")


    For Each b In a.Rows
    If Not IsEmpty(b) Then

    'Start Browsing loop
    ieBrowser.navigate "https://www.brcdirectory.com/InternalSite/Site.aspx?BrcSiteCode=" & b.Value


   dteStartTime = Now
   Do While ieBrowser.READYSTATE <> READYSTATE_COMPLETE
      If DateDiff("s", dteStartTime, Now) > 240 Then Exit Sub
   Loop

   On Error Resume Next
   strDocHTML = ieBrowser.document.getElementById("ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate").innerHTML
   strDocHTML2 = ieBrowser.document.getElementById("ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_Grade").innerHTML

   b.Offset(0, 2).Value = Replace(strDocHTML, "Expiry Date : ", "")
   b.Offset(0, 1).Value = Replace(strDocHTML2, "Grade : ", "")

   End If
   Next b



   ieBrowser.Quit
   Set ieBrowser = Nothing

   Application.ScreenUpdating = True
   Application.DisplayAlerts = True


End Sub
user7415328
  • 1,053
  • 5
  • 24
  • 61