Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save a file from Download File dialog

Status
Not open for further replies.

Gruuuu

Programmer
Oct 7, 2008
543
US
I'm trying to save a csv file which is generated by a database query and accessed through a browser. There is not URL that I can use (or I would be done a long time ago).

I'm using the MSHTML references to navigate through the forms and setting options before submitting. Once I submit, I get the file download prompt. I can use Application.SendKeys to decide to Save or Open the file, but I need to know either: 1) where the file saves to, so I can move/rename it or 2) Open the file and save it from there.

When I do
Code:
Application.SendKeys "o"
the window just... doesn't open(?).

If I do
Code:
Application.SendKeys "s{Enter}"
I'm not able to capture the save location.

I started looking into the SendMessage API, and I've downloaded the AllAPI API-Guide utility, but I'm not really sure what to do with it.

Does anyone have suggestions?
 
Ok, I got it working.

--DISCLAIMER--
Upon researching this problem, I found very little support. The reason why is that this is a security concern. Bypassing the user interactions on a browser violates the security standards (obviously!)

The problem here lies in the fact that files which are not locatable by URL must be collected in this way, if not manually.

I do not hold myself responsible for anyone else's malicious use, and I certainly hope no one else does either!
--END DISCLAIMER--

Ultimately it appears that there are two potential good methods to accept this file, outside of a direct connection to the database that generates it.

My first idea was to use the SendKeys method. My original attempt failed due to timing issues and lack of creativity!

The second method is the SendMessage API. I couldn't figure it out. So I'll leave that alone.

without further ado:
Code:
...
'whatever code that you expect will generate a save file dialog.

[green]'*The generally accepted idea is to wait for the browser's ReadyState to change to READYSTATE_COMPLETE. This will not happen. The readystate waits for the save file dialog to go away. You will need to use the Application.Wait method, without reference to the readystate. Hope and pray that your time interval is long enough.[/green]

  newHour = Hour(Now())
  newMinute = Minute(Now())
  newSecond = Second(Now()) + 2
  waitTime = TimeSerial(newHour, newMinute, newSecond)
  Application.wait waitTime

[green]'Then, the SendKeys method can be used to specify the path AND file name. yay![/green]

  Application.SendKeys keys:="X:\PATH\filename.ext{ENTER}"

[green]'If you're overwriting a file, you will need to wait again and send another key.[/green]

  newHour = Hour(Now())
  newMinute = Minute(Now())
  newSecond = Second(Now()) + 2
  waitTime = TimeSerial(newHour, newMinute, newSecond)
  Application.wait waitTime

  Application.SendKeys keys:="y"

Voila.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top