Results 1 to 4 of 4

Thread: Excel VBA + IE

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Excel VBA + IE

    Hello,

    I have this code that works:
    Code:
    Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText
    But program crashes when it is not present so tried doing it this way and it doesn't work:
    HTML also inside

    Code:
    '<div id="report_165215891151811269_catch"><span class="nodatafound">No Non-Res customer found by the search criteria.</span>
    '</div>
    
    
    Dim nonresidential as Object
    Dim rowj as integer ' this is in a for loop to move through rows
    nonresidential = objIE.document.getElementById("report_165215891151811269_catch")
    If nonresidential Is Nothing Then
    Else
    Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText 
    End If
    Also why is it that html code would produce an odd name like this: report_165215891151811269_catch for every non residential address- just curious?

    Thank you!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA + IE

    for vba, probably should be
    Code:
    set nonresidential = objIE.document.getElementById("report_165215891151811269_catch")
    used to get the job done, but in later versions of ie this no longer works, you may need to loop through a subset of the elements, till you find a match or not, something like

    Code:
    for each ele in objIE.document.All
        if ele.id = "report_165215891151811269_catch" then nonresidential = true: Exit For
    next
    if nonresidential then Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText
    you maybe able to use some tagname to reduce the number of elements to iterate through, depends if the length (count) of the elements in the document as to whether it is relevant
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Re: Excel VBA + IE

    Thanks for the answer. It does work but the program gives a runtime error 424 object required when it encounters an address that is not found in the system with this highlighted in yellow:
    Code:
    Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText
    Code:
    
    When I use On Error Resume Next the program works fine but would like to avoid any potential errors.  Any ideas?  Thank you again
    
     
    Set nonresidential_elements = objIE.document.getElementsByTagName("report_165215891151811269_catch")
    
    ''''''''''''''''' this is what I tried below- doesn't work
               
        'For Each nonresidential In nonresidential_elements
          
         '    If nonresidential.getAttribute("span") = "nodatafound" Then
           '         Cells(rowj, 4) = nonresidential.innerText
          '     End If
        'Next nonresidential
       
     
        '''''''''''''''your code works below but gives runtime error
    
        For Each ele In objIE.document.all
            If ele.ID = "report_165215891151811269_catch" Then nonresidential = True: Exit For
        Next
        If nonresidential Then Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA + IE

    from your code, i am not sure why you should get an error, but if you are running within a loop you would need to set the variable to false before each, if this is not the case, more information is required

    Code:
        nonresidential = False
        For Each ele In objIE.document.all
            If ele.ID = "report_165215891151811269_catch" Then nonresidential = True: Exit For
        Next
        If nonresidential Then Cells(rowj, 4) = objIE.document.getElementById("report_165215891151811269_catch").innerText
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width