VBA to open website and logon with username & password

BrazenMore

New Member
Joined
Apr 14, 2010
Messages
9
Hi
I have the following code which opens a webpage and goes to the username & password fields and logons. However for so strange reason sometimes when the sendkeys sends the username or password it types on the screen incorrectly i.e. the username is brazenmore and sometimes this appears as bbrazenmore or bbrazzzenmore. The code works some of the time any ideas why this happens?

Sub internetlogon()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
'Go to this Web Page!
IE.Navigate "enter web page here"
'Check for good connection to web page loop!
Do
If IE.ReadyState = 4 Then
IE.Visible = False
Exit Do
Else
DoEvents
End If
Loop
'Wait for window to open!
Application.Wait (Now + TimeValue("0:00:01"))
IE.Visible = True
'Send logon information
SendKeys "brazenmore", True
SendKeys "{TAB}", True
SendKeys "brazenmore", True
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

SamTYler

Well-known Member
Joined
Mar 10, 2004
Messages
784
I can't help with VBA Web code, but...

Analyze the HTML code on the login page and use the Form Action URL and the username and password "name"s or "id"s in the "IE.Navigate" line, instead of trying to fill in the blanks in the IE window with SendKey.
 

BrazenMore

New Member
Joined
Apr 14, 2010
Messages
9
Thanks for your reply SamTYler, unfortunately I don't know HTML, the annoying thing is my code works some of the time but I need it to work all the time. Just wondering if anyone has seen this issue with using sendkeys. Its like it repeats typing some of the letters in the username and password
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
SendKeys are probably just below merged cells in the evil list.

They just aren't a good idea.

If you want to automate IE it is possible using VBA.

You probably would need to take a look at the HTML but you don't need to be an expert on it.

All you would probably need is the ID/names of the input elements for the username and password.

I'm afraid though giving specific help on this sort of thing is pretty difficult without further information, specifically the URL.:)
 

BrazenMore

New Member
Joined
Apr 14, 2010
Messages
9
Hi All
I have got the sendkeys to work correctly by adding the line
Application.Wait (Now + TimeValue("0:00:01"))
before entering the username & password.
Now that I've got logged into the webpage I need to select a tab on the webpage(the webpage has 5 tabs across the top and I want to select the second tab). Does anyone know what sendkeys to use to select a tab
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
Do not use SendKeys - you shouldn't need to and could just cause problems.

Here is an ancient example of how to automate IE.
Rich (BB code):
Sub Test()
    Set ie = CreateObject("InternetExplorer.application")
    ie.Visible = True
    ie.Navigate ("https://dwayinstalls.hns.com/forms/viewservice.jsp?id=" & ActiveCell)
    Do
        If ie.ReadyState = 4 Then
            ie.Visible = False
            Exit Do
        Else
            DoEvents
        End If
    Loop
    ie.Document.Forms(0).all("User").Value = "me"
    ie.Document.Forms(0).all("Password").Value = "mypasssword"
    ie.Document.Forms(0).submit
    
End Sub
Note the website in the code doesn't seem to work anymore, but the rest of it is along the right lines.:)
 

snb_

Well-known Member
Joined
Nov 9, 2009
Messages
567
And now written in VBA:

Code:
Sub internetlogon()
   With CreateObject("InternetExplorer.Application")
     .Navigate "http://enter web page here"
     Do until .ReadyState = 4
       DoEvents
     Loop
     DoEvents
     With .document
        .items("username")="brazenmore"
        .items("usernamesend").submit
     End With
  End With
End Sub
<!-- / message -->
 

BrazenMore

New Member
Joined
Apr 14, 2010
Messages
9
Thanks for this Norie it works perfectly
Now I'm trying to select one of the tabs on the page after I login
Im trying this code and its not working
ie.document.forms(0).all("tabname").Select
do you have any ideas?

If you look at the website I sent you and maybe show me the code to use to display the User Policy or Contact Us tabs at the top.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
I'm pretty sure you wouldn't use Select and that part of the page doesn't appear to belong to any form.

It seems to be a link of some sort.

I'm sure I've got some code that would trigger a link but I don't have it to hand.

I'll see if I can find it, give it a try if I do and post back.:)
 

BrazenMore

New Member
Joined
Apr 14, 2010
Messages
9
Hi Norie
Did you happen to find the code to open a link?
I appreciate your help
Thanks
Brazenmore
 

Forum statistics

Threads
1,181,428
Messages
5,929,834
Members
436,697
Latest member
sunnypl

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
Top