vba download and save in C:\user [closed]

2019-07-24 05:38发布

I want excel, using VBA, to download a file from the internet. This file is a database with information about list of employees. I want the file saved in C:\Users\"name of user"\FCMMIS\

This is the step-by-step process that I want to achieve:

  1. Excel using VBA, downloads a file from a specified web address.
  2. *The downloaded file is saved in C:\Users\"name of user"\FCMMIS*
  3. Excel links with the downloaded database via VBA to get data that may be required.

As another note, the excel file in question will be used by different people with laptops. I am not sure if saving the file in the "User" folder is possible or should there needs to be permissions to be given in order to allow a save.

I know that there are better solutions to this like writing a full application but due to the requirement, I am stuck with excel and VBA.

EDIT (to be done after along with part 3):

3.1: Once the database is downloaded, then the excel file gets records from the employee table and prints them in a new worksheet in the same excel file.

3.2: The printed data is then used for various functions in the worksheet.

However, I am in a debate whether is it much efficient to just manipulate the data in the database itself or have it work from the created worksheet.

2条回答
Explosion°爆炸
2楼-- · 2019-07-24 06:31

If the file in question is an workbook you could do this:

With Workbooks.Open("http://example.org/yourfile.xls")
    .SaveAs "C:\Users\example\yourfile.xls"
    .Close
End With
查看更多
Viruses.
3楼-- · 2019-07-24 06:33

The following code is sourced from an article by Matt Vidas

And waiting for your answer to Point 3, no scope?

Function Download_File(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 Testing()
    Download_File "http://example.org/yourDataBaseFile", "C:\Users\" & Environ("username") & "\yourDataBaseFile"
End Sub
查看更多
登录 后发表回答