# Using VBA To Automate IE and input user name, password, and hit submit



## Slape (May 18, 2011)

I'm trying to use VBA to go to a website that requires a User Name, Password, and a Submit Button.

So far I can get everything to work besides the Submit part.  The code runs without errors, but doesn't actually "hit" the submit button on the webpage.  

For posting, I removed my actual user name and password and and used the generic "User Name" and "Password" highlighted in blue.  

I highlighted another section in green that I took from a previous post hoping it would solve my problem.  The link is:  http://www.mrexcel.com/forum/showthread.php?t=317060&highlight=vba+internet+explorer+submit


Here is the code I am using:

Sub GoToWebSiteAndPlayAroundNew()

Dim appIE As Object ' InternetExplorer.Application
Dim URL As String


Set appIE = CreateObject("InternetExplorer.Application")
URL = "https://efolio.morgankeegan.com/escripts/defaultLogon.asp?errCode=2"


With appIE
        .navigate URL
        .Visible = True

            Do While .busy: DoEvents: Loop
            Do While .ReadyState <> 4: DoEvents: Loop

        .document.getelementbyid("fUserName").Value = "UserName"
        .document.getelementbyid("fPassword").Value = "Password"

End With


On Error Resume Next
   x = 0
   For Each mitem In IE.document.all
       mitem.Value = "x"
       x = x + 1
    Next

 x = 0
For Each mitem In IE.document.all
    If x = "Submit" Then
    mitem.Click
    Exit For
    End If

Next

End Sub


----------



## Norie (May 18, 2011)

Are you sure what you need to click is a button.

It could be something like an image. 

I think we would need the URL or at least some of the code to check that.

If you can't post either you could try an alternative approach like the one below. 
If the username and password are part of a form you could try submitting the form.

This should carry out the same action clicking the autumn does.

There are various ways you can get a reference to the form.

If you know the form's you can use getElementByID.

That sometimes works with the name too.

Them there's the forms collection of the document.

Those are probably your best bet but there are other methods you could try.

Once you have a reference to the form you should simply be able to use this.

objFrm.Submit


----------



## Norie (May 18, 2011)

Oops, my bad I didn't notice the URL in the code.

If it's the Enter button you want to click looks like it might be an image.

I can't check the source code right now but I'll have a look later.

PS I don't see'Submit' anywhere.


----------



## Slape (May 18, 2011)

I looked at the source code too, but I'm sort of a beginner when it comes to VBA and I've never tried using VBA with IE.  

I was able to model the user name and password using the getElementById and using their source code of "fUserName" and "fPassword", but wasn't able to figure out the submit part.  I tried using "fReturnPage" with no success.

Maybe part of the problem is that I don't understand the difference between a button and image.


----------



## Norie (May 18, 2011)

Is the URL in the code actually the one you are interested in?


----------



## Norie (May 18, 2011)

I've taken a look at came up with this.

It appears to work, though obviously I get a message about the username/password being wrong.

```
Option Explicit
 
Sub GoToWebSiteAndPlayAroundNew()
Dim appIE As Object    ' InternetExplorer
Dim doc As Object    'HTMLDocument
Dim URL As String
Dim objIMG As Object    ' HTMLImg
Dim objAnchor As Object    ' HTMLAnchorElement
 
    Set appIE = CreateObject("InternetExplorer.Application")
 
    URL = "https://efolio.morgankeegan.com/escripts/defaultLogon.asp?errCode=2"
 
    With appIE
        .navigate URL
        .Visible = True
 
        Do While .busy: DoEvents: Loop
        Do While .ReadyState <> 4: DoEvents: Loop
 
        Set doc = .Document
 
        doc.getelementbyid("fUserName").Value = "UserName"
 
        doc.getelementbyid("fPassword").Value = "Password"
 
        Set objIMG = doc.images("SubmitRus")
 
        Set objAnchor = objIMG.parentElement
 
        objAnchor.Click
 
        Do While .busy: DoEvents: Loop
        Do While .ReadyState <> 4: DoEvents: Loop
 
    End With
 
    Set appIE = Nothing
 
End Sub
```


----------



## Slape (May 18, 2011)

I tried it out and it works perfectly.  I don't quite understand how it works,  but I notice you are using the "images" that you were talking about in your earlier post.  Thank you so much for your help!


----------



## Norie (May 18, 2011)

Glad it worked.

I've never actually used images or parentElement, I didn't even know the former existed.

I though it was going to be far more complicated with all the hidden stuff and possibly having to call/run some script.


----------



## pedie (May 18, 2011)

Hi Norie, sorry to trouble you again but i was try to replicate the same code for this forum as a practice and cant find the login submit button...

```
[FONT=Courier New]doc.getelementbyid("vb_login_username").Value = "Pedie"[/FONT]
[FONT=Courier New]doc.getelementbyid("vb_login_password").Value = "************"[/FONT]
[FONT=Courier New]Set objIMG = doc.images("?????")[/FONT]
```

Could you pls throw some light?

Thanks in advance.


----------



## Norie (May 18, 2011)

Pedie

I can't really help without a URL, there isn't really any generic code you can use this sort of thing.

ie you can't have code that works for one page and just replace the URL and expect the same code to work.

Sometimes you need a few lucky guesses to get things to work, that's sort og what happened here - I guessed it was an image* not a button.

Do you have a URL?

* Actually an image with a URL/anchor/hyperlink.


----------



## Slape (May 18, 2011)

I'm trying to use VBA to go to a website that requires a User Name, Password, and a Submit Button.

So far I can get everything to work besides the Submit part.  The code runs without errors, but doesn't actually "hit" the submit button on the webpage.  

For posting, I removed my actual user name and password and and used the generic "User Name" and "Password" highlighted in blue.  

I highlighted another section in green that I took from a previous post hoping it would solve my problem.  The link is:  http://www.mrexcel.com/forum/showthread.php?t=317060&highlight=vba+internet+explorer+submit


Here is the code I am using:

Sub GoToWebSiteAndPlayAroundNew()

Dim appIE As Object ' InternetExplorer.Application
Dim URL As String


Set appIE = CreateObject("InternetExplorer.Application")
URL = "https://efolio.morgankeegan.com/escripts/defaultLogon.asp?errCode=2"


With appIE
        .navigate URL
        .Visible = True

            Do While .busy: DoEvents: Loop
            Do While .ReadyState <> 4: DoEvents: Loop

        .document.getelementbyid("fUserName").Value = "UserName"
        .document.getelementbyid("fPassword").Value = "Password"

End With


On Error Resume Next
   x = 0
   For Each mitem In IE.document.all
       mitem.Value = "x"
       x = x + 1
    Next

 x = 0
For Each mitem In IE.document.all
    If x = "Submit" Then
    mitem.Click
    Exit For
    End If

Next

End Sub


----------



## pedie (May 18, 2011)

Hi Norie, I was referring to our [this] forum...
I cant make out which one is login submit button + same with facebook..
Username and Password gets inputed then stops because i dont know where to let  vba click on submit...


Thanks again Norie!

links:
1. http://www.mrexcel.com/forum/index.php
2. http://www.facebook.com/


----------



## Norie (May 18, 2011)

Pedie

If you look at the source code of either page you should be able to find what you need.


----------



## pedie (May 18, 2011)

Norie, I did check it out but could not get it...

I found username = vb_login_username
and password = vb_login_password
but cant find submit button name...

??

Thanks again


----------



## Norie (May 18, 2011)

pedie

Is there a specific URL you want to do this for?

You mentioned 'practice', but you can't really do that for this sort of thing.

Anyway for FaceBook the ids for the email and password are simply 'email' and 'pass'.

The login button id is uXXXXXX_X, where X is a digit.

I've not posted the actual digits because I think it might be randomly generated.

Not sure about that though.


----------



## pedie (May 18, 2011)

Great! thanks Norie...
I still cant find for this forum...I mean the submit button, sorry

Pedie


----------



## Norie (May 18, 2011)

Pedie

How are you checking the source code?

By the way, can't you 'practice' on another site?

Oh, almost forgot - the digits for the id for the login button on Facebook are random.


----------



## pedie (May 18, 2011)

Norie said:


> Pedie





Norie said:


> How are you checking the source code?
> 
> By the way, can't you 'practice' on another site?
> 
> Oh, almost forgot - the digits for the id for the login button on Facebook are random.


 

Okay Norie, thanks for letting me know about that...
Here is another link i log on to everyday maybe I can automate this if you help me:https://login.yahoo.com/config/login_verify2?&.src=ym&rl=1


I view the source code my right clicking on the webpage and then click on "View Source" is that the same thing I need to follow?

I just dont know how to identify which letter or letter can mean that it is submit button....


Thanks again!


PS: Is there a way of identifying these things? for example what is what[clickable button, password text box etc?]


----------



## Norie (May 18, 2011)

pedie

Please start a new thread for your own questions.

Posting in an existing thread can confuse people, and even put people off answering your question.


----------



## pedie (May 18, 2011)

Okay Norie, thanks again...this a great learning...
Though i dont actually understand it completely I now know this can be done!


----------



## MardiB16 (Sep 6, 2018)

Hi Norie, I know that the above thread is quite old however, my need for answers is about to drive me batty! I have been searching for a way to automate date entry process from excel to webform. I can get the macro to navigate to the website "http://www.themightygiant.com/cm/index" but can't figure out the code to populate the 'popover' webform. not sure if you or anyone else can help but I'm running out of options. Can anyone help?

I'm Youtubed Out!!!


----------

