1

Using vba and excel, I'm looking to download a file from an url for which I'll not know what the original filename is. Using URLDownloadToFile, or other methods as with WinHttpRequest, expects you to name the file you are downloading. I would like to save with the original filename, as one would do by navigating to the url on internet explorer.

Example of my url: https://mysite.com/PDF/CreatePDFforProcess.aspx?ID=6370947

on Internet explorer, this would open a save as window, with a default filename sugestion.

Many thanks

Community
  • 1
  • 1
Fcoelho
  • 33
  • 1
  • 5
  • Hi Fcoelho, if I understand you, you want to use a VBA script in Excel to navigate a website and download a file. Is that right? – PowerUser Nov 02 '12 at 17:54
  • YES, that's right. I know the URL for the file, I just don't know what filename I will get. I want to use the default filename, as If I was using a browser. Like on this example: http://pdfmyurl.com?url=www.opentracker.net , you will get a pdf file, but you can't tell the type and name from the link. – Fcoelho Nov 02 '12 at 18:04
  • Are you sure you want to use VBA embedded in Excel for this? I'd sooner recommend a Greasemonkey script or using some other kind of browser add in. – PowerUser Nov 02 '12 at 18:24
  • Yes, definitely. I'm developing it on top of an excel tool that uploads XML reports of a calculation. The file I need to download is a validated document of the calculation. Having the functionality in excel, would simplify and greatly reduce the manual workflow of the users. – Fcoelho Nov 02 '12 at 18:34

1 Answers1

1

You can get the file type and file name (sometimes) from the response HTTP header

Example, where I am fetching the file via an XMLHttpRequest object named xhr:

debug.print xhr.getResponseHeader("Content-Type")
>application/pdf
debug.print xhr.getResponseHeader("Content-Disposition")
>attachment; filename="test.pdf"

You should not expect Content-Disposition to always be populated. Since you uploading an input file, it might make more sense for you to name the response file by some variation of the input file's name.

A. Webb
  • 26,227
  • 1
  • 63
  • 95