How to make a Dowload in IE via VBA?

Asked

Viewed 2,766 times

2

Hello I am using the following code to start a dowload in Intenet Explorer.

Public Sub Dashboard()
    Dim Navegador As InternetExplorer
    Dim objShellWindows As New SHDocVw.ShellWindows
    Dim currentWindow As HTMLWindowProxy
    Dim url As String
    
    On Error GoTo Erro
    url = "http://..................../"
    Set Navegador = New InternetExplorer
    Navegador.navigate url
    Navegador.Visible = True

    Do While Navegador.readyState <> 4
        DoEvents
    Loop
    
    'Faz login
    Navegador.document.getElementById("usuario").Value = "xxxxxxxx"
    Navegador.document.getElementById("senha").Value = "xxxxxx"
    Navegador.document.getElementById("bt_entrar").Click
'    Set Navegador = Nothing
'    Set Navegador = New InternetExplorer
'    Navegador.navigate url
'    Navegador.Visible = True
    
    Do While Navegador.readyState <> 4
        DoEvents
    Loop
    
    Navegador.document.getElementById("submenu2_SatisfacaoGeral").Click
    Set currentWindow = Navegador.document.parentWindow
    currentWindow.execScript code:="chart.exportChart()"

Erro:
    MsgBox Err.Description
    Resume Next
 End Sub

turns out I don’t know how I can "save as download" by setting the folder and the file name.

  • Ever tried to use this method?

  • 1

    This does not suit my case, the script I run, already starts the Download, I need to manipulate the IE window by changing file name and save location. De;Set JanelaSite = IE.document.parentWindow&#xA; JanelaSite.execScript code:="chart.exportChart()"&#xA; &#xA; Application.Wait (Now + TimeValue("0:00:02"))&#xA; JanelaSite.Focus&#xA; 'JanelaSite.document.execCommandShowHelp ("SaveAs")&#xA; tela.SendKeys "{TAB}"&#xA; tela.SendKeys "{TAB}"&#xA; tela.SendKeys "{DOWN}"&#xA; tela.SendKeys "{DOWN}"&#xA; tela.SendKeys "{ENTER}"

  • 1

    I don’t know about IE, but if it’s useful, I found some links: 1, 2,3

1 answer

0

You can interact with the PDF, for example by downloading it from the URL created for a specified folder, using the Urldownloadtofile function,

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 test()
        URL = Navegador.LocationURL 'URL do pdf
        DownloadFile URL, "C:\PASTA\EXEMPLO.pdf" 'Pasta e nome do arquivo
    End Sub
  • Hello Marcelo, I will test here and tell you. I even managed without using ". Sendkeys", but I couldn’t change the file name and still ran into another problem, IE started to insert (random) Numbers in the file name (I think by the download number). That’s why I had to create a PHP page and managed to do everything I wanted.

  • I tried here, the problem is that in my case the command I run already starts the download. I wanted to manipulate the Download window to change the file name and location.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.