sexta-feira, 18 de setembro de 2020

Website login using excel vba

Do you need to constantly login, introduce username and password into a website in order to check updated data? 

For example, if your are working with account receivables, do you need to check your company´s bank accounts to confirm if new deposits are available? And for that you have to input username and password manually every time you go to the bank website? Depending on the bank website, there is a timeout that is applied of 3 or 5 minutes and the website automatically logouts, so if you check the bank website 20 times, you have to input the username and password 20 times manually and it is prone to error. 

To avoid the manual input of the credentials and the potential typing error, you can use excel with vba macro to automate the login process and it is valid for all websites that requires username and password.

Will show an example of login to a account in the Facebook website using excel and vba macro. What the macro does it is to automate the login process: creates a Internet Explorer windows and fills the username and password fields and clicks the login button.

For that, on the Visual Basic window, you need to include some libraries, go to References and select  the following library: "Microsoft Internet Controls"




After including this reference create or copy this procedure on VBA and fill with your username and password.

Sub SearchBotLogin()
    
    'declare an Internet Explorer Object
    Dim objIE As InternetExplorer
    
    'create a new Internet Object
    Set objIE = New InternetExplorer
    
    'Set the Explorer to VIsible
    objIE.Visible = True
    
    'fill the Excel status bar with some progess information
    Application.StatusBar = "Login"
 
    'navigate IE to the web page
    objIE.navigate "https://www.facebook.com/"
 
    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
 
    'fill the username in the username field id
    objIE.document.getElementById("email").Value = "username"
    
    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    'fill the username in the password field id
    objIE.document.getElementById("pass").Value = "password"
    
    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
     
    'click the 'Login' button
    objIE.document.getElementById("u_0_f").Click
    
    'wait again for the browser
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
     
    'fill the Excel status bar with some progress information
    Application.StatusBar = "Login OK"

End Sub
The elements for username, password and login button can be obtained using the inspection tool on the Internet Explorer


Sem comentários:

Enviar um comentário

PDFsam Basic an open source solution for manipulating pdfs files

Do you work with a lot of pdfs files, do you need to split, merge, extract pages, mix and rotates PDF files?  PDFsam Basic is the solution,...