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

Slape

New Member
Joined
Oct 23, 2008
Messages
49
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
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
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
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
Is the URL in the code actually the one you are interested in?
 
Upvote 0
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.
Rich (BB code):
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
 
Upvote 0
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!
 
Upvote 0
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.
 
Upvote 0
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...
Code:
[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.:)
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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