Excel vba to access Open/Save dialog

viktiw

Board Regular
Joined
Jan 3, 2015
Messages
54
Hello everyone,
I am trying to download a document from a website using vba.
So far I can click the print icon on website (using vba) but I am stuck when it shows Open/Save dialog box.
Please help on how I can access or click the "Save" button and save file to a given location.

Any help is appreciated.

-Vik
 

Attachments

  • OpenSave.jpg
    OpenSave.jpg
    17.9 KB · Views: 21

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
If you have a URL to that actual document, you don't need a browser to actually download the file.

VBA Code:
Option Explicit
#If VBA7 Then
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As LongPtr, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As LongPtr, ByVal lpfnCB As LongPtr) As Long
#Else
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
#End If

Public Function DownloadFile(ByVal URL As String, ByVal DestinationFile As String) As Boolean
    ' If the API returns ERROR_SUCCESS (0), DownloadFile returns True.
    DownloadFile = (URLDownloadToFile(0&, URL, DestinationFile, &H10, 0&) = CLng(ERROR_SUCCESS))
End Function

This will need to be put into a new standard module, but then can be called from anywhere with a simple:

VBA Code:
DownloadFile DocumentURL, "D:\TEMP\SaveDocumentHere.Doc"

Does that help?
 
Upvote 0
I also tried Application. SendKeys "%{S}" (Save) method.
If Send key is the only method, how can I use Save As
If you have a URL to that actual document, you don't need a browser to actually download the file.

VBA Code:
Option Explicit
#If VBA7 Then
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As LongPtr, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As LongPtr, ByVal lpfnCB As LongPtr) As Long
#Else
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
#End If

Public Function DownloadFile(ByVal URL As String, ByVal DestinationFile As String) As Boolean
    ' If the API returns ERROR_SUCCESS (0), DownloadFile returns True.
    DownloadFile = (URLDownloadToFile(0&, URL, DestinationFile, &H10, 0&) = CLng(ERROR_SUCCESS))
End Function

This will need to be put into a new standard module, but then can be called from anywhere with a simple:

VBA Code:
DownloadFile DocumentURL, "D:\TEMP\SaveDocumentHere.Doc"

Does that help?
Hi Dan,
Thank you for your reply.
I checked, the print icon when clicked, displays the open/save dialog box but doesn't have the url to document.
The url for print icon is same as the main page of website.
I am thinking of using send keys.
Application.SendKeys"{S}"
 
Upvote 0
For the reasons you've discovered ("How do I SaveAs"), Sendkeys is probably not the best approach.
I note your point about the URL being the same as the main page - that sounds like the URL is being generated by Javascript. If you're using Internet Explorer, is there a "Copy Shortcut" option available when you right click on the hyperlink/print button? Failing that, you could always check out John's approach: IE Automation to Save as File Download from website with LogIn
 
Upvote 0

Forum statistics

Threads
1,214,818
Messages
6,121,725
Members
449,049
Latest member
MiguekHeka

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top