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

Asked

Viewed 1,252 times

1

I can’t click a button that generates an Excel report (then it opens that "File Download" button "Want to save? Open? Cancel? but this part is quiet, I think, if it’s not let me know and if you already have the solution...).

Problem is I 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 those worked, and I changed the properties over and over like 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 the methods, using the "Click" work, the code runs, but... does not open the file download tab, as if it did not click the button, that is, it finds the object/button, just does not execute the script I think. It is the last step, practically for this macro, the process will be 100% automatic with this.

1 answer

0

In your HTML I don’t see a button called download_token_value only one input of the kind hidden that has no event click associated with it, nor should it, as it is a hidden element on the page.

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

Thus:

'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 folder C:\:

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

Don’t forget to go on Tools > References and select the items:

  • Microsoft WinHTTP Services
  • Microsoft ActiveX Data Objects 2.8 library

Browser other questions tagged

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