0

Problem : I am trying to automate the saving of a file (manage to save the file when the below IE bar appears at the bottom of the page)

My code is going on my intranet, clicking here and there and then I click on an "Export" button which will trigger this from IE :enter image description here

I didn't manage to find a way to automate the saving of the file because the only way (I think) to interact with this "window" is to use SendKeys. In order to do so I have to "activate" this window (Yes, I have it activated for the HTML scraping with this bit of code, but it's not the active window though):enter image description here

I tried using AppActivate but for some reason it doesn't work.

There are 2 options to pass this obstacle :

  • Find a way to activate the IE window containing that saving bar so that I can use Application.SendKeys "%{S}" on it
  • (second option only) : Disable this saving bar or manage to have it open in a new window

So far I have gone through lots of posts talking about that subject but none gave an operational solution for that issue on IE 11.

Let me know if you want to see any bit of code, I have a lot of different attempts gathered from different posts but this would highly increase the length of this post.

Seb
  • 508
  • 8
  • 25
  • This could be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you trying to achieve? Is the purpose of your code to download something from an internal website? – David Rushton Apr 12 '17 at 12:17
  • Yes I am trying to automate the download process of this .xls file as you can see on the picture above. I'll update the post to make it clearer – Seb Apr 12 '17 at 12:21
  • Does the IE window containing that save bar always have the same caption, next to the IE logo? Does the download always have the same URL? – David Rushton Apr 12 '17 at 12:34
  • the URL and Title of the IE window are always the same yes and the URL(hidden part) of the download bar is also the same. Only the report number varies (aka file name) – Seb Apr 12 '17 at 12:38
  • Just test IE11 and send keys. `%{S}` worked for me. You said that AppActivate didn't work. Did it raise an error? – David Rushton Apr 12 '17 at 12:55
  • When I send this key nothing happens so it's just skipped. Then with the AppActivate "TitleOfMyPage" I get a runtime error 5 even though I took precaution to make sure it's the right title by putting it in an If condition – Seb Apr 12 '17 at 12:58
  • There was also someone who suggested to send `SendKeys "{F6}", True SendKeys "{TAB}", True SendKeys "{ENTER}", True` I don't remember the post though. But again it works only when I manually activate the window – Seb Apr 12 '17 at 13:05

1 Answers1

0

Front-end automation is a tricky business. It can be really hard to find a solution that always works (those pesky users are free to move the mouse and click buttons at will, disrupting your code). Because you are taking the data from an intranet site, this suggests the data you need already exists within your organisation. If at all possible (and I know it isn't always) take the data from the servers/source systems, rather than via the UI.

AppActivate and SendKeys can be very fussy. Because the url is always the same a better approach would be to directly download it. Here is a example, based on another answer.

Before you can run code you will need to add two references:

  1. Microsoft XML, v6.0
  2. Microsoft ActiveX Data Objects 2.8 Library

From the VBA IDE click Tools >> References... and select from the list (the original answer does not use references, instead it uses late binding).

' Downloads a file from a given URL.
'   URL     String      URL to get file.  Ex: http:\\MySite\MyFile.
'   Path    String      Location to save file.  Ex: C:\Myfile.csv.
' Requires: Microsoft XML, v6.0
' Requires: Microsoft ActiveX Data Objects 2.8 Library.
Sub DownloadURL(ByVal URL As String, ByVal Path As String)
    Dim httpXML As XMLHTTP60            ' Used to download data.
    Dim st As ADODB.Stream              ' Used to write downloaded data to file system.
    Set httpXML = New XMLHTTP60

    ' Request file.
    httpXML.Open "GET", URL, False
    httpXML.send

    ' Download response.
    If httpXML.Status = 200 And httpXML.readyState = 4 Then

        Set st = New ADODB.Stream
        st.Open
        st.Type = adTypeBinary
        st.Write httpXML.responseBody
        st.SaveToFile Path, adSaveCreateOverWrite
        st.Close
    Else

        MsgBox "Something went wrong :(", vbCritical, "An error occured"
    End If
End Sub 
Community
  • 1
  • 1
David Rushton
  • 4,915
  • 1
  • 17
  • 31
  • Sorry for all the questions, but this kind of automation is much harder that you would expect! – David Rushton Apr 12 '17 at 13:21
  • Yeah I'm still confused honestly. I tried again to send F6 + Tab + Enter but this time with a `Application.Wait Now + TimeSerial(0, 0, 1)` before and after and it works too...seems like it is random – Seb Apr 12 '17 at 13:23
  • That's quite common. It's really easy for code to get ahead of the UI. The problem is wait times differ. Sometimes Windows need half a second to update the screen. Sometimes 3. You cannot predict this. If you set wait too low, sometimes it'll fire too early. If you set it too high, the user has time to start clicking. SendKeys can never be bullet proof, I'm afraid. – David Rushton Apr 12 '17 at 13:30