{"id":11,"date":"2014-08-28T19:00:15","date_gmt":"2014-08-28T19:00:15","guid":{"rendered":"http:\/\/www.phillippeng.com\/?p=11"},"modified":"2014-12-27T05:52:23","modified_gmt":"2014-12-27T05:52:23","slug":"excel-vba-download-url-file-to-local-drive","status":"publish","type":"post","link":"http:\/\/www.phillippeng.com\/?p=11","title":{"rendered":"excel VBA: Download URL file to local drive"},"content":{"rendered":"<p>VBA code to download a file from URL address:<\/p>\n<pre lang=\"vb\">Sub DownloadUrlFile()\r\n\r\n' Create an array to hold the response data.\r\nDim arrDownloadedBytes() As Byte\r\nDim WinHttpReq As WinHttpRequest\r\nDim strURL, strLocalPath, strLocalFileName As String\r\n\r\nstrURL = \"https:\/\/www.xxxx.com\/documents\/FilenameToDownload\"\r\n\r\nstrLocalPath = \"C:\\User\\documents\\\"\r\nstrLocalFileName = \"FilenameDownloaded\"\r\n\r\nSet WinHttpReq = CreateObject(\"WinHttp.WinHttpRequest.5.1\")\r\nWinHttpReq.SetAutoLogonPolicy (0)\r\n\r\n'In case you have V5.0 instead of v5.1 - use this line\r\n'Set WinHttpReq = CreateObject(\"WinHttp.WinHttpRequest.5.0\")\r\n\r\n' create the HTTP Request\r\nWinHttpReq.Open \"GET\", strURL, False\r\n\r\n' Send the request\r\nWinHttpReq.Send\r\n\r\n' copy the response body to a local file\r\nOpen strLocalPath &amp; strLocalFileName For Binary As #1\r\narrDownloadedBytes() = WinHttpReq.ResponseBody\r\nPut #1, 1, arrDownloadedBytes()\r\nClose\r\n\r\nEnd Sub\r\n\r\n\u00a0\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>VBA code to download a file from URL address: Sub DownloadUrlFile() &#8216; Create an array to hold the response data. Dim arrDownloadedBytes() As Byte Dim WinHttpReq As WinHttpRequest Dim strURL, strLocalPath, strLocalFileName As String strURL = &#8220;https:\/\/www.xxxx.com\/documents\/FilenameToDownload&#8221; strLocalPath = &#8220;C:\\User\\documents\\&#8221; strLocalFileName = &#8220;FilenameDownloaded&#8221; Set WinHttpReq = CreateObject(&#8220;WinHttp.WinHttpRequest.5.1&#8221;) WinHttpReq.SetAutoLogonPolicy (0) &#8216;In case you have V5.0 instead [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry","category-excel"],"_links":{"self":[{"href":"http:\/\/www.phillippeng.com\/index.php?rest_route=\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.phillippeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.phillippeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.phillippeng.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.phillippeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11"}],"version-history":[{"count":4,"href":"http:\/\/www.phillippeng.com\/index.php?rest_route=\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":17,"href":"http:\/\/www.phillippeng.com\/index.php?rest_route=\/wp\/v2\/posts\/11\/revisions\/17"}],"wp:attachment":[{"href":"http:\/\/www.phillippeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.phillippeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.phillippeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}