Macro Vba - IE ... Click, Not working button

1

I am not able to click on a button that generates a report in Excel (after it opens that "File Download" tab "Do you want to save? Open? Cancel ?, but this part is quiet, I think, if not me and if they already have the solution ...).

Problem is tried several ways:

IE.Document.getElementById("download_token_value").removeAttribute("onClick") 'FAILED
IE.Document.getElementById("download_token_value").setAttribute "onClick", "return true" 'FAILED
IE.Document.getElementsByName("DownloadToken").Item.onclick 'FAILED

For Each Button In IE.Document.getElementsByTagName("DownloadToken") 'FAILED
    Button.Click
Next
    IE.Document.getElementById("download_token_value").submit 'FAILED
    IE.Document.getElementById("download_token_value").Click 'FAILED
    IE.Document.all("download_token_value").Click 'FAILED
    IE.Document.getElementById("download_token_value").Click 'FAILED
    IE.Document.getElementsByName("DownloadToken").Item.Click 'FAILED

None of these worked, and I changed properties several times as I said:

  </ul>
</div>

<form action="/RptManifestoStatus/Index" method="post"><input id="download_token_value" name="DownloadToken" type="hidden" value="" />
<div class ="toolbar">
  <div class ="toolbarBorder">
    <div class="toolbarItem">
      <input class="btExportarExcel" src="/Content/images/pix.gif" style="vertical-align: bottom; border:0;" type="image" value="" />
    </div>
  </div>
</div>    
<div class="boxContent">
  <div id="tabs">
    <ul>

Another observation, all methods, using "Click" work, the code runs, but ... does not open the file download tab, as if it did not click the button, ie it finds the object / button , just does not run the script I think. It is the last step, practically for this macro, the process will be 100% automatic with that.

    
asked by anonymous 13.09.2017 / 15:31

1 answer

0

In your HTML, I do not see any buttons called download_token_value only a input of type hidden that does not have any click associated with it, nor should it, since it is a hidden element on the page. p>

If your field, according to what I'm seeing, has the URL to download, why not download it directly instead of sending it to another window?

So:

'Pode ser Public ou Private, depende do seu projeto
Private Function BaixarArquivo(strURL As String, strCaminhoArquivo As String, enumOpcoesGravacao As SaveOptionsEnum) As Boolean
    On Error GoTo ErrHandle

    Dim objStream As New ADODB.Stream
    Dim objWinHttpReq As New WinHttp.WinHttpRequest

    'Acessa a URL via GET de maneira síncrona
    objWinHttpReq.Open "GET", strURL, False
    objWinHttpReq.send

    With objStream
        If objWinHttpReq.Status = 200 Then
            .Open
            .Type = adTypeBinary
            .Write objWinHttpReq.responseBody
            .SaveToFile strCaminhoArquivo, enumOpcoesGravacao
            .Close
        End If
    End With

    Set objStream = Nothing
    Set objWinHttpReq = Nothing
ErrHandle:
    If Err.Number = 0 Then
        BaixarArquivo = True
    Else
        Debug.Print "Erro número:" & Err.Number & " | " & Err.Description
        Err.Clear
        BaixarArquivo = False
    End If
End Function

And call the function, for example, to download a pdf in the C:\ folder:

If BaixarArquivo(IE.Document.getElementById("download_token_value").Value, "C:\Arquivo.pdf", adSaveCreateOverWrite) Then
    Msgbox "Download concluído"
End If

Do not forget to go to Tools > References and select the items:

  • Microsoft WinHTTP Services
  • Microsoft ActiveX Data Objects 2.8 library
21.09.2017 / 14:35