Download a file to local drive with VBA

Posted on 2005-04-07
Last Modified: 2008-03-17
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!

Question by:earthman100
    LVL 119

    Accepted Solution

    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
        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
                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
    LVL 48

    Assisted Solution

    by:Ryan Chong

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
    I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now