IE Automation to Save as File Download from website with LogIn

johnmpc

Board Regular
Joined
Oct 19, 2020
Messages
108
Office Version
  1. 365
Platform
  1. Windows
Hi Guru's,

I have the below code working to automate the download starting process, but i can't for the life of me work out the Methods explained and code provided on several posts relating to the automation of the Save AS Process.
Please would someone kindly Help me.

"C:\Pricelists\Ralawise\" Is the target file location.
RalawisePrice is the desired filename.

I will need it to overwrite or Kill the existing files in the location too.

Thank you.


VBA Code:
Sub RalawiseAIE()


'to refer to the running copy of Internet Explorer
Dim IE As InternetExplorer


'open Internet Explorer in memory, and go to website
Set IE = New InternetExplorer


With IE
        .Visible = True
        .Navigate ("https://shop.ralawise.com/")
        While .Busy Or .ReadyState <> 4: DoEvents: Wend
            
            
      
    With .Document
    If .querySelectorAll("button#login").length > 0 Then
        .getelementbyid("EmailAddress").Value = "John.G@MPCembroidery.co.uk"
        .getelementbyid("Password").Value = "*Password*" 'Not Real Password
        .getelementbyid("login").Click
    
     Else
    End If
    End With
    
    .Navigate ("https://shop.ralawise.com/globalassets/webdatadownloads/distinctsku.zip")
   
    
End With
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
@John_w I think you might be the expert on this type of thing.
Would greatly appreciate your help.
Thanks.
 
Upvote 0
Does your macro successfully log into the web site and is https://shop.ralawise.com/globalassets/webdatadownloads/distinctsku.zip a direct link to the file you want to download?

If so try this macro:
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon.dll" Alias "URLDownloadToFileA" _
        (ByVal pCaller As LongPtr, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As LongPtr) As Long
    Private Declare PtrSafe Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias "DeleteUrlCacheEntryA" _
        (ByVal lpszUrlName As String) As Long
#Else
    Private Declare Function URLDownloadToFile Lib "urlmon.dll" Alias "URLDownloadToFileA" _
        (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
    Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias "DeleteUrlCacheEntryA" _
        (ByVal lpszUrlName As String) As Long
#End If

Private Const BINDF_GETNEWESTVERSION As Long = &H10



Sub RalawiseAIE()

    'to refer to the running copy of Internet Explorer
    Dim IE As InternetExplorer
   
    'open Internet Explorer in memory, and go to website
    Set IE = New InternetExplorer
   
    With IE
        .Visible = True
        .navigate "https://shop.ralawise.com/"
        While .Busy Or .readyState <> 4: DoEvents: Wend
         
        With .document
            If .querySelectorAll("button#login").Length > 0 Then
                .getElementById("EmailAddress").Value = "John.G@MPCembroidery.co.uk"
                .getElementById("Password").Value = "*Password*" 'Not Real Password
                .getElementById("login").Click
            End If
        End With
       
        While .Busy Or .readyState <> 4: DoEvents: Wend
       
        DownloadFile "https://shop.ralawise.com/globalassets/webdatadownloads/distinctsku.zip", "C:\Pricelists\Ralawise\RalawisePrice.zip"
       
    End With
   
End Sub


Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
   
    Dim retVal As Long
   
    DeleteUrlCacheEntry URL
    retVal = URLDownloadToFile(0, URL, LocalFilename, BINDF_GETNEWESTVERSION, 0)
    If retVal = 0 Then DownloadFile = True Else DownloadFile = False

End Function
 
Upvote 0
Unfortunately i think its a button that has to be pressed to start the download. That side of the code works.
I can get the download to start, its just the automation of the SAVE AS pop up box and naming the file.
 
Upvote 0
1642601348484.png
 
Upvote 0
Unfortunately i think its a button that has to be pressed to start the download. That side of the code works.
I can get the download to start, its just the automation of the SAVE AS pop up box and naming the file.
In that case, try incorporating this code which uses UIAutomation to automate the IE 'Save As' file download dialogue:


Copy and paste the code marked UIAutomation download function into a new module, and add the reference noted at the top of the code.

Then incorporate the code from Test routine into your macro, something like this:

VBA Code:
Sub RalawiseAIE()

    Dim saveAsFullName As String
    Dim replaceExistingFile As Boolean
    Dim downloadResult As String, downloadStatus As Boolean
    
    saveAsFullName = "C:\Pricelists\Ralawise\RalawisePrice.zip"
    replaceExistingFile = True
    
    'to refer to the running copy of Internet Explorer
    Dim IE As InternetExplorer
    
    'open Internet Explorer in memory, and go to website
    Set IE = New InternetExplorer
    
    With IE
        .Visible = True
        .navigate "https://shop.ralawise.com/"
        While .Busy Or .readyState <> 4: DoEvents: Wend
          
        With .document
            If .querySelectorAll("button#login").Length > 0 Then
                .getElementById("EmailAddress").Value = "John.G@MPCembroidery.co.uk"
                .getElementById("Password").Value = "*Password*" 'Not Real Password
                .getElementById("login").Click
            End If
        End With
        
        While .Busy Or .readyState <> 4: DoEvents: Wend
        
    End With

    'After IE offers the file download
    
    downloadStatus = IE_Download_File_Using_UIAutomation(IE.hwnd, saveAsFullName, replaceExistingFile, downloadResult)
    Debug.Print "Download result = " & downloadResult
    Debug.Print "Download saveAsFullName = " & saveAsFullName
    Debug.Print "Download status = " & downloadStatus
    
End Sub
 
Upvote 0
Solution
Getting typed defined errors on the
VBA Code:
Private Sub DumpElement(UIAutoElem As IUIAutomationElement)
private function
on the UIAutomation download function.

Do i have something in the wrong place maybe?
I out the complete code from that link from the box under UIAutomation download function in a new module.
And put the other code in a separate module.
 
Upvote 0
@John_w Thanks for your help AGAIN!!

Had to add in the redirection URL after logging in and take out the wait but got it working spot on. I suspect i will use this code alot!!
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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