Code works in Debug mode but will not run through when played

joshuaatwork

New Member
Joined
Feb 28, 2013
Messages
13
I put together about 10 separate macros that will log you into a site using the values in a given cell. I was having trouble with the last one because of AutoComplete remembering the username. So I put in an IF statement, and when I used F8 to go line for line, it works perfectly. However, when I click F5 and just let it play through, it doesn't log in. I tried adding a 5 second delay, but that didn't seem to work. I still get a run-time error when I hit play.

It get hung on this line:
Code:
If doc.getElementById("user_name").Value = cUsername Then

But the complete code is this:
Code:
Sub StreetLinks_Login()

'On Error Resume Next

    Dim cURL As String
    cURL = Worksheets("Sheet1").Range("B9").Value
    Dim cUsername As String
    cUsername = Worksheets("Sheet1").Range("D9").Value
    Dim cPassword As String
    cPassword = Worksheets("Sheet1").Range("E9").Value
    
    Dim ie As InternetExplorer
    Dim doc As HTMLDocument
    Dim LoginForm As HTMLFormElement
    Dim UserNameInputBox As HTMLInputElement
    Dim PasswordInputBox As HTMLInputElement
    Dim SignInButton As HTMLInputButtonElement
    Dim HTMLelement As IHTMLElement
    Dim qt As QueryTable

    Set ie = New InternetExplorer
    
    ie.Visible = True
    ie.Navigate cURL
    
    'Wait for initial page to load
    
    Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy: DoEvents: Loop
    
    Set doc = ie.document
    
    'Get the only form on the page
    
    Set LoginForm = doc.forms(0)
    
    'Get the User Name textbox and populate it
    'input name="Email" id="Email" size="18" value="" class="gaia le val" type="text"
    
    Set UserNameInputBox = doc.getElementById("user_name")
    

    If doc.getElementById("user_name").Value = cUsername Then

        Else
            UserNameInputBox.Value = cUsername
    End If
    
    
    'Get the password textbox and populate it
    'input name="Passwd" id="Passwd" size="18" class="gaia le val" type="password"

    Set PasswordInputBox = doc.getElementById("password")
    PasswordInputBox.Value = cPassword
    
    'get the form input button and click it
    'input class="gaia le button" name="signIn" id="signIn" value="Sign in" type="submit"
    
    doc.getElementById("btnLogin").Click
    
    'Wait for the new page to load
    
    Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy: DoEvents: Loop
    
'On Error GoTo 0
    
    End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Sorry, I forgot. Because the URL is not in the code, here is the URL I'm going to:
http://pipefire.streetlinks.com

<colgroup><col width="341"></colgroup><tbody>
</tbody>
 
Upvote 0
This works for me, although the web page displays invalid username/password, as expected.
Code:
Sub StreetLinks_Login()
    
    Dim IE As InternetExplorer
    Dim doc As HTMLDocument
    Dim UserNameInputBox As HTMLInputElement
    Dim PasswordInputBox As HTMLInputElement

    Set IE = New InternetExplorer
    
    IE.Visible = True
    IE.Navigate "https://pipefire.streetlinks.com/Pipefire/Login.aspx"
    
    'Wait for initial page to load
    
    Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    Set doc = IE.document
    
    Set UserNameInputBox = doc.getElementById("user_name")
    UserNameInputBox.Value = "user1234"

    Set PasswordInputBox = doc.getElementById("password")
    PasswordInputBox.Value = "password999"
    
    doc.getElementById("btnLogin").Click
    
    'Wait for the new page to load
    
    Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
End Sub
If autocomplete is a problem, try clearing the input first with:
Code:
UserNameInputBox.Value = ""
I can't help further because I don't have an account on that site.
 
Upvote 0
I've tried clearing the value first but it just creates an error. Without logging in, would you be able to try something for me? Even though you can't log in, the site should remember your username. Can you try running the code when there is a autocompleted username and see if clearing the value gives you an error like it gives me?

This code should work, but it still pulls an error.
Code:
Sub StreetLinks_Login()

    Dim cURL As String
    cURL = Worksheets("Sheet1").Range("B9").Value
    Dim cUsername As String
    cUsername = Worksheets("Sheet1").Range("D9").Value
    Dim cPassword As String
    cPassword = Worksheets("Sheet1").Range("E9").Value
    
    Dim ie As InternetExplorer
    Dim doc As HTMLDocument
    Dim LoginForm As HTMLFormElement
    Dim UserNameInputBox As HTMLInputElement
    Dim PasswordInputBox As HTMLInputElement
    Dim SignInButton As HTMLInputButtonElement
    Dim HTMLelement As IHTMLElement
    Dim qt As QueryTable

    Set ie = New InternetExplorer
    
    ie.Visible = True
    ie.Navigate cURL
    
    'Wait for initial page to load
    
    Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy: DoEvents: Loop
    
    Set doc = ie.document
    
    'Get the only form on the page
    
    Set LoginForm = doc.forms(0)
    
    Set UserNameInputBox = doc.getElementById("user_name")
    
    UserNameInputBox.Value = ""
    UserNameInputBox.Value = cUsername
        
    End Sub
 
Upvote 0
My IE8 doesn't remember the username for that site, even though AutoComplete is enabled, so I can't reproduce the problem you describe.
 
Upvote 0
That's so strange, I'm using IE9 and it remembers my username even when I ask it not to. Thank you anyway for your help.
 
Upvote 0
Figured it out. So the main problem was with IE remembering the username. I had tried an IF statement to determine if the field was not null or not null and ran into issues. No matter what I tried I couldn't clear the form first. So I decided to try it with SendKeys. I populated the password field first, then did a shift+tab then delete to clear the username. Now it's working perfectly. Thanks John_w for the help.
 
Upvote 0

Forum statistics

Threads
1,214,853
Messages
6,121,935
Members
449,056
Latest member
denissimo

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