Professional Documents
Culture Documents
copy
Home Blog Contents Excel Development Advertise Here ERTAT Add-In Suggested Books About Disclaimer F.A.Q.
3
Gosto
Website Log-In Automation With VBA
Tweet
Written by Christos Samaras
on
Wednesday, 21 January 2015
at
00:46
5
Partilhar
Subscribe with your e-mail!
Submit
Introduction
In the last few months I saw a lot of people struggling to automate the log-in procedure to various websites using VBA. To
Search this blog...
be honest, web-related tasks can be considered as advanced VBA topics, since in many cases require basic knowledge of
HTML language. In this post I will try to provide some insights about how to automate the log-in procedure via VBA. More
Top 3 Youtube videos
precisely, I will analyze the concept behind a reusable macro that I developed. The macro creates a new instance of
Internet Explorer, navigates to the desired webpage, enters the username and password values in the corresponding text Create & Open KML KMZ Files With Google Maps &
boxes of the webpage, and, finally, presses the sign-in button in order to complete the log-in procedure. Google Earth
http://www.myengineeringworld.net/2015/01/website-log-in-automation-vba-macro.html[07/01/2018 21:49:17]
Website Log-In Automation With VBA ~ My Engineering World
Almost half of the code of “WebsiteLogIn” macro is used to start a new instance of Internet Explorer and navigate to the The integration between Excel and text
files is still an important issue. The inp...
requested URL. Since late binding is used, it is necessary to use the CreateObject method and ensure that the object was
created. The ShowWindow API is used to maximize the Internet Explorer window. Before the main procedure starts, the VBA Macro To Open A PDF File
IsURLValid function is used to find if the target URL exists (see more about IsURLValid below). If yes, the procedure The previous week, while I was preparing
continues, otherwise an error message pops up. a presentation, I encountered the
following problem: how could I open a
PDF file from a power ...
The other half code is dominated by the usage of getElementById method, which returns a reference to an HTML element
by using its ID. So, let’s say a few HTML things: When an HTML document is loaded into a web browser, it becomes a Numerical Integration In Excel Using The
document object. The document object is the root node of the HTML document and the "owner" of all other nodes Trapezoidal Rule
(element nodes, text nodes, attribute nodes, and comment nodes). The document object provides properties and methods The Basics A common complaint about
Excel is that it doesn’t provide a direct
to access all node objects (using JavaScript for example). One of the available document object methods, which is used in
method to calculate the integral of a
this example, is the getElementById method.
function. If the...
The getElementById method is used to find 3 elements on the target webpage: the username and password text boxes, as
well as the sign-in button. This information is user-input, along with the URL of the webpage, the username and the
password for logging-in. But, how to find the element IDs so as to use this macro? Fortunately, with nowadays web
browsers this is not a difficult task; you just have to follow the next 7 steps:
1. Open your favorite web browser and navigate to the website you want to automate the logging-in procedure.
3. On the context menu that pops up, select the “Inspect Element” option.
4. At the bottom of the webpage a new window will appear containing the HTML code of the webpage.
5. In the highlighted line(s) of the HTML code try to find the id property.
7. Repeat this procedure (steps 2 to 6) for all three elements (username text box, password text box and sign-in button).
The getElementById method was preferred in the particular case instead of other document object methods, such as
almost always assign unique ID values to the HTML elements of the webpages that they design. Returning to the
"WebsiteLogIn" macro, if the getElementById finds the user-input ID of the element, it will assign a value if the element is a
(username/password) text box, or it will invoke the click method if the element is a (sign-in) button. In any case, if the
element ID is not found within the DOM (Document Object Model) of the webpage an error message will pop up, informing
B. WebsiteLogIn macro
Option Explicit
http://www.myengineeringworld.net/2015/01/website-log-in-automation-vba-macro.html[07/01/2018 21:49:17]
Website Log-In Automation With VBA ~ My Engineering World
'Declaring the necessary ShowWindow API function and the constant to maximize Internet
Explorer window.
#If VBA7 And Win64 Then
#Else
#End If
'---------------------------------------------------------------------------------
-----------------------------------------
'This macro can be used in order to log-in to a website automatically. It requires
6 parameters, which are analyzed below.
'The macro creates a new instance of Internet Explorer, navigates to the desired
site, enters the username and password
'values in the corresponding text boxes and presses the sign-in button in order to
log-in to the site.
'The code uses late binding, so no reference to external library is required.
'Required parameters:
'URL: The website URL you want to log-in. Example:
https://login.yahoo.com/?.src=ym&done=https%3a//mail.yahoo.com
'UNElementID: The element ID of the text box, in which you write the UserName.
Example: login-username
'UserName: The username that is used for log-in. Example: MyUserName
'PWElementID: The element ID of the text box, in which you write the Password.
Example: login-passwd
'Password: The password that is used for log-in. Example: MyPassword
'SIElementID: The element ID of the button that you press in order to log-in.
Example: login-signin
http://www.myengineeringworld.net/2015/01/website-log-in-automation-vba-macro.html[07/01/2018 21:49:17]
Website Log-In Automation With VBA ~ My Engineering World
MsgBox "Sorry, the URL you provided is not valid!", vbCritical, "URL Error"
Exit Sub
End If
'Create a new Internet Explorer instance, make it visible and maximize its window.
On Error Resume Next
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
ShowWindow IE.hwnd, SW_MAXIMIZE
http://www.myengineeringworld.net/2015/01/website-log-in-automation-vba-macro.html[07/01/2018 21:49:17]
Website Log-In Automation With VBA ~ My Engineering World
Set IE = Nothing
End Sub
C. IsURLValid function
The code for the IsURLValid function follows. Note that this function could be also used as a built-in function. If you have
various websites or individual webpages and you need to check if their URLs are valid you can use this function. In the
workbook that you will find on the Downloads section I have implemented this technique in order to add the function’s
'--------------------------------------------------------------------
'Checks if a URL is valid; returns True if exists and False if not.
'Create the WinHttpRequest object and check if the object was created.
Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
If Err.Number <> 0 Then
MsgBox "Could not create the WinHttpRequest object!", vbCritical,
"WinHttpRequest Error"
Exit Function
End If
End Function
D. Sample macros
http://www.myengineeringworld.net/2015/01/website-log-in-automation-vba-macro.html[07/01/2018 21:49:17]
Website Log-In Automation With VBA ~ My Engineering World
Finally, there are 3 sample macros that use the “WebsiteLogIn” macro in order to log-in to Gmail, Yahoo mail and
Facebook. In all macros (“GmailLogIn”, “YahooMailLogIn” and “OtherLogIn”) I used the 7-step procedure that was
described above in order to get the correct element IDs from the corresponding webpages.
Option Explicit
Sub GmailLogIn()
WebsiteLogIn
"https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.co
m/mail", _
"Email", Sheets("Log-In").Range("C6").Value, _
"Passwd", Sheets("Log-In").Range("C8").Value, "signIn"
End Sub
Sub YahooMailLogIn()
WebsiteLogIn "https://login.yahoo.com/?.src=ym&done=https%3a//mail.yahoo.com", _
"login-username", Sheets("Log-In").Range("C13").Value, _
"login-passwd", Sheets("Log-In").Range("C15").Value, "login-
signin"
End Sub
Sub OtherLogIn()
'Sample usage of WebsiteLogIn macro for logging-in to any page, as long as the
necessary
'parameters have been specified (in the Log-In sheet we use Facebook as an
example).
With Sheets("Log-In")
End With
End Sub
The “Gmail” and “Yahoo” headings of the workbook I think that are self-explanatory. If you have an account to these email
providers simply enter your username and password and press the corresponding log-in button. In the “Other” heading
http://www.myengineeringworld.net/2015/01/website-log-in-automation-vba-macro.html[07/01/2018 21:49:17]
Website Log-In Automation With VBA ~ My Engineering World
Start by entering the URL of the website you need to log-in. The IsURLValid function at the adjacent cell will return
TRUE if the URL you entered exists (the cell next to the URL will become either blue, or red – if the URL doesn’t exist).
Next, following the 7-step procedure that was described in the VBA code section, find the element IDs of the username
and password text boxes, as well as the ID of the sign-in button. Enter these ID values in the corresponding cells of the
worksheet.
Finally, enter your username and password and press the Other Log-In button.
With the latter procedure you can quickly test any website you wish to log-in. In the sample workbook for example I have
included the IDs of the Facebook log-in webpage, just to demonstrate that the same approach can be used almost in
every webpage that requires log-in.
Finally, if you are wondered how the asterisks appear on the cells that contain usernames and passwords just right click
on any of them and on the context menu that will appear select Format Cells → go to the Number tab → select the
Custom category → look at the format type that is applied, which is this: ;;;**. Nice trick, don’t you think?
Demonstration video
The short video demonstrates the result of the "WebsiteLogIn" macro, as well as shows the 7-step procedure that was
described above.
http://www.myengineeringworld.net/2015/01/website-log-in-automation-vba-macro.html[07/01/2018 21:49:17]
Website Log-In Automation With VBA ~ My Engineering World
Downloads
The file can be opened with Excel 2007 or newer. Please enable macros before using it.
Did you like this post? If yes, then share it with your friends. Thank you!
Christos Samaras
Mechanical Engineer (Ph.D. cand.), M.Sc. Cranfield University, Dipl.-Ing. Aristotle University, Thessaloniki - Greece.
Communication: e-mail,
Facebook,
Twitter,
Google+
and Linkedin.
More info here.
http://www.myengineeringworld.net/2015/01/website-log-in-automation-vba-macro.html[07/01/2018 21:49:17]
Website Log-In Automation With VBA ~ My Engineering World
Copyright 2011 - 2017: Christos Samaras Follow us on Facebook, Twitter, Google+, Linkedin, Youtube
& with your
email
http://www.myengineeringworld.net/2015/01/website-log-in-automation-vba-macro.html[07/01/2018 21:49:17]