Download a file to local drive with VBA

I would like to know how I might download an XML file from an internet website and save it to the local hard drive, with another name, please.  Do I have to open the file and re-write the file locally, or is there a way to directly download and rename it using VBA?  Thanks!

earthman100Asked:
Who is Participating?
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
you need to add to your references  
Microsoft Internet Controls

copy this function and save to a module

Function PageContents(sURL As String) As String
On Error Resume Next
Dim iz As New InternetExplorer
iz.Navigate sURL
Do Until iz.Busy = False
    DoEvents
Loop
    PageContents = CStr(iz.Document.Body.innerhtml)

end function


create a form with a command button and copy this codes to the form code module
Option Compare Database
Option Explicit
Dim fName As String, sDump As String

Private Sub Command0_Click()
Dim PCText As String, sDump As String
PCText = PageContents("http://www.websiteName")
sDump = PCText

If sDump <> "" Then
        Call DumpPCText(fName, sDump)
        MsgBox "File save :  " & fName
        Else
            MsgBox "Please try the operation again"

        Exit Sub
End If
End Sub


Sub DumpPCText(fName As String, sDump As String)

fName = "C:\TextFiles\NameOfFile.txt"    

Dim fHandle
fHandle = FreeFile

Open fName For Output As fHandle
'Open fName For Append As fHandle
Print #fHandle, sDump

Close fHandle

End Sub
0
All Courses

From novice to tech pro — start learning today.