Innertext the "src" address using MSXML2.xmlHttp

Estevam Guilherme

New Member
Joined
Jul 16, 2015
Messages
9
I’m trying to innertext a specific "src" address using MSXML2.xmlHttp. But, when I try sending the request with xmlhttp I get no error but it also give me nothing in return. How can I solve that?

Code:
Sub WebData()
    Dim obj As Object, resp As String
    With CreateObject("MSXML2.xmlHttp")
        .Open "GET", "stURL", False
        .send
        resp = .responseText
    End With
 
    With CreateObject("HTMLFile")
        .write resp
        For Each obj In .all.getElementsByTagName("img")
            If obj.Content Like "src:/*/" Then
                MsgBox obj.Content
            End If
        Next
    End With
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi,
You have to use real URL like "https://www.mrexcel.com/" instead of "stUrl"
For example like this:
Rich (BB code):
Sub WebData()
   
    Dim s As String, obj As Object, resp As String
   
    With CreateObject("MSXML2.xmlHttp")
        .Open "GET", "https://www.mrexcel.com/", False
        .send
        resp = .responseText
    End With
 
    With CreateObject("HTMLFile") 
        .write resp
        For Each obj In .Body.Document.getElementsByTagName("img")
           s = obj.OuterHTML
           Debug.Print s ' you may parse s for "src=*" here
        Next
    End With
   
End Sub
Regards
 
Last edited:
Upvote 0
Hello Mr.!

in order to reach my goal I used a real url as you mentioned. It almost brings me all "img" addresses...except the one I want. Maybe it's because its location is between a "table".

HTML:
[TABLE]
<tbody>[TR]
[TD="class: line-content"] <table summary="Box per effettuare il login a sistema, inserire username e password." style="float: right">[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            <caption>Login a sistema</caption>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            <tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td scope="row">[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <label for="UserName" id="UserNameLabel">Nome Utente (E-mail):</label>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <input name="UserName" type="text" maxlength="255" id="UserName" accesskey="l" tabindex="1" title="Nome Utente (E-mail)" class="myInput" style="width:170px;" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            </tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            <tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td scope="row">[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <label for="Password" id="PasswordLabel">Password:</label>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <input name="Password" type="password" maxlength="50" id="Password" tabindex="2" title="Password" class="myInput" style="width:170px;" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            </tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            <tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <label for="loginCaptcha" id="LblCapt">Codice di verifica:</label>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td scope="row">[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <input name="loginCaptcha" type="text" id="loginCaptcha" tabindex="3" title="Codice di verifica:" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <img id="captchaLogin" src="[URL="https://prenotaonline.esteri.it/captcha/default.aspx?pos=2&vers=361676769"]captcha/default.aspx?pos=2&vers=361676769[/URL]" alt="Codice di verifica" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            </tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            <tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <input type="submit" name="BtnPwdDimenticataL" value="Ho dimenticato la password" id="BtnPwdDimenticataL" tabindex="5" title="Ho dimenticato la password" class="linkButton" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td style="text-align: right">[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <input type="submit" name="BtnConfermaL" value="Login" id="BtnConfermaL" tabindex="4" title="Login" class="inputButton" style="width:170px;" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            </tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                        </table>[/TD]
[/TR]
</tbody>[/TABLE]
[HTML]
 
Last edited:
Upvote 0
Then you may parse text of the variable resp for "src:/*/", like this:
Rich (BB code):
    ' ...
   
    Dim i As Long, j As Long
    i = InStr(resp, "src:/")
    While i > 0
      j = InStr(i + 5, resp, "/")
      If j Then
        s = Mid(resp, i, j - i + 1)
        Debug.Print s
      End If
      i = InStr(j + 1, resp, "src:/")
    Wend
 
Last edited:
Upvote 0
ZVI, today I got this also working, but I'm not able to outerHTML into excel, could you please give me a light.

[Dim s As String, obj As Object, resp As String

With CreateObject("HTMLFile")
.write sResp

For Each obj In .body.document.getElementsByTagName("img")
Debug.Print obj.outerHTML
Next


End With]
 
Last edited:
Upvote 0
Sure, try this:
Rich (BB code):
Sub WebData()
 
  Dim s As String, obj As Object, resp As String
   
  ' Put content of a web page to the resp string variable
  With CreateObject("MSXML2.xmlHttp")
    .Open "GET", "https://www.mrexcel.com/", False
    .send
    resp = .responseText
  End With
   
  ' Clear A-column
  ActiveSheet.UsedRange.EntireRow.Columns(1).ClearContents
 
  ' Put a title to A1 cell
  Range("A1").Value = "WebData"
   
  ' Write outerHTMLs below the A1 cell
  With CreateObject("HTMLFile")
    .write resp
    For Each obj In .body.document.getElementsByTagName("img")
      s = obj.outerHTML
      Range("A" & Rows.Count).End(xlUp).Offset(1).Value = s
      'Debug.Print s
    Next
  End With
 
End Sub
 
Last edited:
Upvote 0
Thank you ZVI, everything is working just fine. I’m quite ok with IE automation. But I’m new with MSXML2.xmlHttp and like the way you work with.
 
Upvote 0
Thank you ZVI, everything is working just fine. I’m quite ok with IE automation. But I’m new with MSXML2.xmlHttp and like the way you work with.
You are welcome, we are all learners here!
:beerchug:
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top