none
Download File from IE with VBA

    Question

  • Hello, I've created a VBA macro within excel that points to a webpage using the InternetExplorer object. From here I wish to click on a button that will download a .csv file. Right now clicking on the button causes the information bar to popup due to the security settings. It appears that the button has a java script behind the scenes that actually creates the .csv file, but IE is blocking my access. I'm hoping to do one of the following to get around this but don't know how to go about it: 1.) Figure out how to disable the information bar programatically (perhaps by changing the security settings temporarily, or by telling the info bar to allow access) 2.) Try to mimic right clicking the button and use a "Save Target As" approach. Does anyone know how to go about this? If this is in the wrong forum please let me know. Thanks!
    Monday, August 24, 2009 6:00 PM

All replies

  • I'd like to know the URL of the page.
    Monday, August 24, 2009 10:35 PM
  • Unfortunately this is a password protected site, so at this point in my process I've already navigated through several screens of security.  Essentially the object is an HTMLLinkElement but the file I need to download seems to be created through a java script or something.  Do you need any other information?
    Monday, August 24, 2009 11:10 PM
  • hi

    Several years ago I would often get some files on the web using VBA.
    I hope that it is helpful.

    The following code get a MP3 file from an URL.

    Declare Function URLDownloadToFile Lib "urlmon" Alias _
        "URLDownloadToFileA" (ByVal pCaller As Long, _
        ByVal szURL As String, _
        ByVal szFileName As String, _
        ByVal dwReserved As Long, _
        ByVal lpfnCB As Long) As Long

    Function DownloadFile(URL As String, LocalFilename As String) As Boolean
        Dim lngRetVal      As Long
        lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
        If lngRetVal = 0 Then DownloadFile = True
    End Function

    Sub downFLash()
        Dim sURL    As String
        Dim LocalFilename   As String
        Dim filename As String
       
        Const UNC = "C:\Documents and Settings\All Users\Documents\My Music\"
       
        filename = "20050628091444_1.mp3"
        sURL = "http://jrc.donga.com/nihao_service/200506/" & filename
        LocalFilename = UNC & filename

        Debug.Print DownloadFile(sURL, LocalFilename)
       
    End Sub

    HTH
     
    the best time to plant a tree was twenty years ago. the second best time, is today - Chinese proverb sjoo.kwak at gmail.com
    • Edited by SJOO Tuesday, August 25, 2009 12:21 AM
    Monday, August 24, 2009 11:13 PM
  • I see.  I think my problem may be a bit trickier since the URL to my file is hidden.  Right now the the URL looks something like www.mywebpage/downloadfile#

    It puts the "#" in for the file name because the HTML code is calling a java script that is behind the scenes. 
    Monday, August 24, 2009 11:18 PM
  • This worked like a charm for me.  Thanks!!!!
    Tuesday, November 02, 2010 4:12 AM
  • hi

    Several years ago I would often get some files on the web using VBA.
    I hope that it is helpful.

    The following code get a MP3 file from an URL.

    Declare Function URLDownloadToFile Lib "urlmon" Alias _
        "URLDownloadToFileA" (ByVal pCaller As Long, _
        ByVal szURL As String, _
        ByVal szFileName As String, _
        ByVal dwReserved As Long, _
        ByVal lpfnCB As Long) As Long

    Function DownloadFile(URL As String, LocalFilename As String) As Boolean
        Dim lngRetVal      As Long
        lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
        If lngRetVal = 0 Then DownloadFile = True
    End Function

    Sub downFLash()
        Dim sURL    As String
        Dim LocalFilename   As String
        Dim filename As String
       
        Const UNC = "C:\Documents and Settings\All Users\Documents\My Music\"
       
        filename = "20050628091444_1.mp3"
        sURL = "http://jrc.donga.com/nihao_service/200506/" & filename
        LocalFilename = UNC & filename

        Debug.Print DownloadFile(sURL, LocalFilename)
       
    End Sub

    HTH
     
    the best time to plant a tree was twenty years ago. the second best time, is today - Chinese proverb sjoo.kwak at gmail.com
    rubbish
    Monday, July 25, 2011 7:54 AM
  • To download files from VBA in Excel you can use the following code:

    VBA Excel: Download files

    Tuesday, November 01, 2011 6:16 PM