Thanks Thanks:  0
Likes Likes:  0
Page 1 of 2 12 LastLast
Results 1 to 10 of 20

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

  1. #1
    New Member
    Join Date
    Oct 2008
    Posts
    49
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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/showthr...xplorer+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

  2. #2
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,623
    Post Thanks / Like
    Mentioned
    44 Post(s)
    Tagged
    4 Thread(s)

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

    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
    If posting code please use code tags.

  3. #3
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,623
    Post Thanks / Like
    Mentioned
    44 Post(s)
    Tagged
    4 Thread(s)

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

    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.
    If posting code please use code tags.

  4. #4
    New Member
    Join Date
    Oct 2008
    Posts
    49
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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.

  5. #5
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,623
    Post Thanks / Like
    Mentioned
    44 Post(s)
    Tagged
    4 Thread(s)

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

    Is the URL in the code actually the one you are interested in?
    If posting code please use code tags.

  6. #6
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,623
    Post Thanks / Like
    Mentioned
    44 Post(s)
    Tagged
    4 Thread(s)

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

    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.
    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/escr....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
    If posting code please use code tags.

  7. #7
    New Member
    Join Date
    Oct 2008
    Posts
    49
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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!

  8. #8
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,623
    Post Thanks / Like
    Mentioned
    44 Post(s)
    Tagged
    4 Thread(s)

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

    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.
    If posting code please use code tags.

  9. #9
    Board Regular pedie's Avatar
    Join Date
    Apr 2010
    Location
    INDIA
    Posts
    3,875
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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:
    doc.getelementbyid("vb_login_username").Value = "Pedie"
    doc.getelementbyid("vb_login_password").Value = "************"
    Set objIMG = doc.images("?????")
    Could you pls throw some light?

    Thanks in advance.
    Regards,
    Pedie
    MS OFFICE 2016/EXCEL 2016: Window7: 64BIT
    Visit YouTube.com/VBAa2z


  10. #10
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    74,623
    Post Thanks / Like
    Mentioned
    44 Post(s)
    Tagged
    4 Thread(s)

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

    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.
    If posting code please use code tags.

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •