Option Explicit
Function SaveWebFile(ByVal vWebFile As String, ByVal vLocalFile As String) As Boolean
Dim oXMLHTTP As Object, i As Long, vFF As Long, oResp() As Byte
'You can also set a ref. to Microsoft XML, and Dim oXMLHTTP as MSXML2.XMLHTTP
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP")
oXMLHTTP.Open "GET", vWebFile, False 'Open socket to get the website
oXMLHTTP.Send 'send request
'Wait for request to finish
Do While oXMLHTTP.readyState <> 4
DoEvents
Loop
oResp = oXMLHTTP.responseBody 'Returns the results as a byte array
'Create local file and save results to it
vFF = FreeFile
If Dir(vLocalFile) <> "" Then Kill vLocalFile
Open vLocalFile For Binary As #vFF
Put #vFF, , oResp
Close #vFF
'Clear memory
Set oXMLHTTP = Nothing
End Function
Sub XLScode()
Dim wb As Workbook
Dim urlXls As strng, filePath As String
urlXls = "your web path to excel file"
filePath = "C:\test.xls"
SaveWebFile urlXls, filePath
Set wb = ActiveWorkbook.Open(filePath)
End Sub
Sub PDFcode()
Dim urlPDF As String, filePath As String
urlPDF = "your web path to pdf file"
filePath = "C:\test.pdf"
SaveWebFile urlPDF, filePath
End Sub
|