VBA to click the save button in the IE save as file dialog box

  • Hi all,


    I will have a second try to see if I can manage to click with VBA the OK button in the Save As dialog box from Internet Explorer.


    Now I have a message box appearing to stop the code for a moment while I choose the file to download, choose the folder where to save to and click the OK button.
    It would be great if I could include in the macro code a part where once the message box appears, it will wait until the Internet Explorer popup asking where to save and click save.
    It should always use the same folder to save the files.


    1- is there a way to make excel wait for Internet Explorer to pop up the dialog box?
    2- tell it where to save
    3- click the save button and continue with the code.


    I found some code google-ing, but these are specific for a page and and not that easy for me to understand.


    Greetings.

  • Re: VBA to click the save button in the IE save as file dialog box


    Get the new file name by using a Dir function loop with an incrementing counter variable to see if "file_<counter>.xxx" exists and exit the loop when it doesn't. Then pass the new file name to the Download_File routine.

  • Re: VBA to click the save button in the IE save as file dialog box


    Hi John,


    I was a few days absent and could not have a look at it yet.
    I tried the macro this morning (the one I have so far working), but noticed a little mistake. In the first run it will get the correct file name to save as, on the second run he will put the folder path into the save as dialog box. Then it will give me an error that the file name is not correct.


    I saw your replay about using the Dir function, but as my knowledge of vba is basic, no clue if I will manage this. Maybe you can give me some tips how to get this on done.


    I found the below code in internet, maybe there is a way to fit it somewhere:



    Greetings.

  • Re: VBA to click the save button in the IE save as file dialog box


    This gets the next file_n.xxx file name:

    Code
    1. Dim folderPath As String, fileName As String, n As Integer
    2. folderPath = "C:\path\to\folder\" 'CHANGE THIS
    3. n = 0
    4. Do
    5. n = n + 1
    6. fileName = Dir(folderPath & "file_" & n & ".xxx")
    7. Loop Until fileName = ""
    8. MsgBox "Next file is " & "file_" & n & ".xxx"
  • Re: VBA to click the save button in the IE save as file dialog box


    Hi John,


    I managed to put your code between the code I have so far, but I don't think I did it correct.


    Can you please let me know where I went wrong on below code?



    Thanks in advance.

  • Re: VBA to click the save button in the IE save as file dialog box


    Just change my code to use your variable names:

  • Re: VBA to click the save button in the IE save as file dialog box


    Hi John,


    I'm using the code from you as I found in this link:
    http://www.mrexcel.com/forum/excel-q...cations-3.html


    I only changed the folder where it should be saved. I'm lost in where I should make the change. I tried the above code, but it does not leave the same file name and changes it for file file_1xxx.xlm (or whatever the extesion happens to be). for some reason it wants to make them all file_1xxx. The rest of your code is the same as that from the link as I only made the changes in the code of your previous answer.


    Greetings.

  • Re: VBA to click the save button in the IE save as file dialog box


    My code above finds the next file name in the sequence "file_1.xxx", "file_2.xxx", etc. Have you changed the "file" part to whatever is your actual file name, not the word "file" and the "xxx" part to whatever is your actual file extension, e.g. "xls", "xlm", etc? Try stepping through the code line by line by pressing F8 on each line in the VBA editor, examine variables, add debug.print statements or MsgBox prompts.

  • Re: VBA to click the save button in the IE save as file dialog box


    Hi John,


    The file name comes actually from the intranet site it self. In some ocasions there are files that have always the same name and the extension could be .xls or .xml.
    When I click on the file to download, the save as dialog box from internet explorer pops up and there is already the file name in it. That what I need is, that when the file name is the same, it should add a number behind it so all the files that go in the folder will be unique and the macro will not overwrite them. The full code that I found in the link works great, but I have only the problem when the file name is the same while downloading.
    Maybe the code needed is different as it probably needs to first check the folder what is there, and if the same file name is there, it should add an incrementing number to avoid to overwrite or the code stop working because of the file names.


    Sorry for my silly questions and explanations, I just that I don't know better :-(


    Greetings.

  • Re: VBA to click the save button in the IE save as file dialog box


    This code in http://www.mrexcel.com/forum/e…ations-3.html#post2805320

    You need to pass "" as the saveAsFilename argument to IE_Navigate_and_Download so that the filename = Get_Window_Text(hWnd) line is executed. Then incorporate my other code at that point (with a few tweaks) to find the file name with the next sequential number.

  • Re: VBA to click the save button in the IE save as file dialog box


    Hi megatronixs,


    I would like to know if you managed to run the "save as dialog box from Internet Explorer macro" created by John_w in Windows 7 & IE 11? I tried it but it will not click the "Save/Save As" buttons. If you were able to use it, please share the code.


    Thanks,
    Chirs