Skip to content

Instantly share code, notes, and snippets.

@kumatti1
Created October 19, 2019 22:11
Show Gist options
  • Save kumatti1/4a1e1eac32aa059f96d982f069003b16 to your computer and use it in GitHub Desktop.
Save kumatti1/4a1e1eac32aa059f96d982f069003b16 to your computer and use it in GitHub Desktop.
VBAでXHR2
Option Explicit
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Main()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = True
ie.navigate "https://www.google.co.jp/"
While ie.busy Or ie.readyState <> 4
Sleep 1&
Wend
Dim win As MSHTML.IHTMLWindow5
Set win = ie.document.parentWindow
Dim tmp As HTMLXMLHttpRequestFactory
Set tmp = win.XMLHttpRequest
Dim XHR As Object 'HTMLXMLHttpRequestでなく、「XHR2」なので、IDispatchで受ける。
Set XHR = tmp.Create
Dim B() As Byte
With XHR
'/index.htmlが無効なので適当な引数を付加。
.Open "GET", "#" & Timer, False
.responseType = "ArrayBuffer"
.send
B = .response
With CreateObject("ADODB.Stream")
.Open
.Type = 1
.write B
.SaveToFile CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\hoge.html", 2
.Close
End With
End With
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment