Excel/VBAでマクロでPOSTした結果をファイルに落とす

ExcelのWebクエリ機能と同じ事を(GETじゃなくて)POSTで出来ないかと思って。
DLしたファイルを外部データとして参照するマクロがあれば、POSTして得られたデータをExcelに落とせる。

根性があればWebを巡回して新しい入札情報だけ取ってきたり出来ますな!


Option Explicit

' thx to
' id:ardarim http://q.hatena.ne.jp/1171410367
' http://blog.goo.ne.jp/ghostwind/e/87b0af815fb2a7bc7ec9b6e4a781021f

Sub test()

If GetUrl("http://example.com/hogehoge.php", "c:\posttest.html") Then
'MsgBox "成功"
Else
MsgBox "失敗"
End If

End Sub

Function GetUrl(ByVal url As String, ByVal filename As String) As Boolean

Dim i As Long, l As Long
Dim fn As Integer
Dim html As String
Dim b() As Byte


Dim WinHttp As New WinHttpRequest ' winhttp.dll
' Dim WinHttp As Object
' Set WinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")



WinHttp.Open "POST", url, False
WinHttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WinHttp.Send "name=22&name2=33"

If WinHttp.Status < 200 And WinHttp.Status > 399 Then
GetUrl = False
Exit Function
End If

l = LenB(WinHttp.ResponseBody)
fn = FreeFile()
Open filename For Binary Access Write As #fn
html = WinHttp.ResponseBody
ReDim b(l)
For i = 1 To l
b(i - 1) = AscB(MidB(html, i, 1))
Next i
Put #fn, , b
Close #fn

GetUrl = True
On Error GoTo 0
Exit Function

errorhandler:
If fn <> 0 Then Close #fn
GetUrl = False
On Error GoTo 0

End Function