1

I have been seeking to validate bulk URL and content in it. And print the value in B2 cell range.

Sub CheckPageData()
 
    Dim cell As Range
    Dim IntExp As Object
    
    Set IntExp = CreateObject("InternetExplorer.Application")
    IntExp.Visible = True
 
    For Each cell In Range("A2:A10")
        'Here A2 is cell Address where we have stored urls which we need to test.
        
        If Left(cell.Value, 4) = "http" Then
            
            ' Goto web page
            IntExp.Navigate cell.Text
            
           ' Below loop will run until page is fully loaded
            Do Until IntExp.ReadyState = 4 
            Loop
 
            ' Now use text which you want to search , error text which you want to compare etc.
            If InStr(IntExp.Document.body.innerText, _
               "Text which you want to search or verify") > 0 Then
               
               cell.Offset(, 1).Value = "Result message which you want to give."
 
            Else
               If InStr(IntExp.Document.body.innerText, _
               "The page you requested was not found.") > 0 Then
                  cell.Offset(, 1).Value = "The page you requested was not found."
               End If
 
            End If
        End If
    Next cell
 
    IntExp.Quit
    Set IntExp = Nothing 
End Sub
 
ZygD
  • 22,092
  • 39
  • 79
  • 102
MIB
  • 35
  • 1
  • 1
  • 6
  • You are missing an `End If` right before the other `End If`s – braX Sep 08 '21 at 01:01
  • Can you edit and show ? – MIB Sep 08 '21 at 01:04
  • Right after `cell.Offset(, 1).Value = "The page you requested was not found."` put `End If` – Tim Williams Sep 08 '21 at 01:15
  • `Range("A2", 5)` is not a valid Range – Tim Williams Sep 08 '21 at 01:16
  • @mohammed No one can fix that for you since we do not know what is the intended range, what range are you trying to loop? `A2:A5`? – Raymond Wu Sep 08 '21 at 01:30
  • Hey thanks for help, but I want to print value into cell how do I do that? – MIB Sep 08 '21 at 01:30
  • I have changed cell range A2:A10 – MIB Sep 08 '21 at 01:38
  • `I want to print value into cell` - What value? and what cell? You have assigned value to a cell before in your code so I doubt that's what you are asking... @mohammedBaig – Raymond Wu Sep 08 '21 at 01:46
  • @Raymond Wu , I am looking for VBA Script to get status code of provided link (A2) and inner content from same from (B2). And paste the value of status code in (C2) – MIB Sep 08 '21 at 01:51
  • Unforunately your lack of details is not helping us as well, what `status code` are you looking for? Error `404` or OK `200` this kind? It will be best if you can provide an example with 1 URL and what are you expecting to see from that URL? Provide a screenshot of it @mohammedBaig – Raymond Wu Sep 08 '21 at 02:01
  • @RaymondWu link for example is racaty.net/1dvfju36wuym i want to grab data from popup and place the same in excel – MIB Sep 08 '21 at 02:15

1 Answers1

1

Since you did not give sufficient information on what you actually wants, this will loop through A2:A10 and retrieve the text in the popup (that can be found by using class name file-info) and insert in column B:

Sub CheckPageData()
 
    Dim cell As Range
    Dim IntExp As Object
    
    Set IntExp = CreateObject("InternetExplorer.Application")
    IntExp.Visible = True
 
    For Each cell In Range("A2:A10")
        'Here A2 is cell Address where we have stored urls which we need to test.
        
        If Left(cell.Value, 4) = "http" Then
            
            ' Goto web page
            IntExp.Navigate cell.Text
            
           ' Below loop will run until page is fully loaded
            Do While IntExp.Busy Or IntExp.readyState <> 4
                DoEvents
            Loop
 
            ' Now use text which you want to search , error text which you want to compare etc.
            Dim ieDoc As Object
            Set ieDoc = IntExp.Document
            
            If ieDoc.getElementsByClassName("file-info").Length <> 0 Then
                cell.Offset(, 1).Value = ieDoc.getElementsByClassName("file-info")(0).innerText
            End If
        End If
    Next cell
 
    IntExp.Quit
    Set IntExp = Nothing
End Sub
Raymond Wu
  • 3,357
  • 2
  • 7
  • 20
  • I am getting error "Method 'Navigate' of object 'ÍwebBrowser2' failed – MIB Sep 08 '21 at 02:44
  • Try `IntExp.Navigate cell.value` – Raymond Wu Sep 08 '21 at 02:56
  • I am using this link **https://racaty.net/1dvfju36wuym** in Cell A2 & class name ID **text-danger** – MIB Sep 08 '21 at 03:08
  • IntExp.Navigate cell.value did not help same error – MIB Sep 08 '21 at 03:09
  • not helpful, did you get a chance to identify the issue – MIB Sep 08 '21 at 03:17
  • The code is working for me so the issue is on your end. – Raymond Wu Sep 08 '21 at 03:26
  • https://stackoverflow.com/questions/30086425/excel-vba-method-document-of-object-iwebbrowser2-failed Might be an issue with your Internet Explorer – Raymond Wu Sep 08 '21 at 03:29
  • https://stackoverflow.com/questions/63217045/method-document-of-object-iwebbrowser2-failed-on-a-dedicated-company-server or try this. It's not possible to tell you what's your issue since the code is working on my end. @mohammedBaig – Raymond Wu Sep 08 '21 at 03:29
  • dear @RaymondWu thanks the issue was in Internet Explorer. Request your feed back on one more issue how to I added multiple **getElementsByClassName** in the same program as I would be validating multiple links from different domain. – MIB Sep 08 '21 at 10:32
  • Usually you will need a sub dedicated to a webpage as each webpage is different, however if you are sure that all the links that you are validating is as simple as the case above (just need to check 1 instance of classname and they are all unique across all links), I suppose you can do multiple If-ElseIf-EndIf statements to check through all the predefined classnames. @mohammedBaig – Raymond Wu Sep 08 '21 at 10:50
  • If this answer resolves this question, pleas accept it by clicking the tick beside the answer. (You can only accept 1 answer if there are multiple answers submitted) @mohammedBaig – Raymond Wu Sep 08 '21 at 10:56
  • thanks, will update and check if anything will trouble you again. Much appreciate for your help. – MIB Sep 08 '21 at 10:57
  • Please post a new question for that. – Raymond Wu Sep 08 '21 at 11:03
  • Thanks Raymond issue is resolved by introducing If statement multiple times – MIB Sep 08 '21 at 12:53