asleepdirka
New Member
Hello all,
I have a challenging VBA macro that attempts to Login to an intranet website. Using the code below (credit to a previous poster), I am able to navigate to the login webpage and populate the Username and Password fields, however, I cannot get the Submit portion to function. I have attempted previous forum examples without success. Through hours upon hours of trial and error, I am left stumped, as well as my team of IT professionals at my company. The problem seems to be the Form ID's not being a simple Username and Password, but not sure. Any advice or assistance would be greatly appreciated!!!
Below the VBA code below I pasted the View Source Website information.
'Needs references to Microsoft HTML Object Library and Microsoft Internet Controls
Option Explicit
Sub Test()
Const cURL = "https://mywebsite¬.com/login.aspx"
Const cUsername = "XXXX" 'REPLACE XXXX WITH YOUR USER NAME
Const cPassword = "YYYY" 'REPLACE YYYY WITH YOUR PASSWORD
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
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="ctl00$ct$UserName" type="text" maxlength="30" id="ctl00_ct_UserName" style="width:160px;" />
Set UserNameInputBox = LoginForm.elements("ctl00_MainContent_containerMain_loginMain_UserName ")
UserNameInputBox.Value = cUsername
'Get the password textbox and populate it
'< input name="ctl00$ct$Password" type="password" maxlength="30" id="ctl00_ct_Password" style="width:160px;" />
Set PasswordInputBox = LoginForm.elements("ctl00_MainContent_containerMain_loginMain_Password ")
PasswordInputBox.Value = cPassword
'Get the form input button and click it
'< input type="submit" name="ctl00$ct$uxBtnLogin" value="Sign In" o n c l i c k="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00_MainContent_containerMain_loginMain_btnLogin ", "", true, "Login", "", false, false))" id="ctl00_ct_uxBtnLogin" />
Set SignInButton = LoginForm.elements("ctl00$ct$uxBtnLogin")
SignInButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.busy: DoEvents: Loop
'Get the HTML document of the new page
Set doc = IE.document
'Determine whether login succeeded or not
If InStr(doc.body.innerText, "Invalid Login Information") = 0 Then
MsgBox "Login succeeded"
Else
MsgBox "Login failed"
End If
Debug.Print "Current URL: " & IE.LocationURL
End Sub
________________________________________________ WEBSITE HTML VIEW SOURCE________________________
Log In Required
</div>
<div class="LoginBox">
Please log in to access your Report-IT! site.
<div> </div>
<table border="0" cellpadding="4" cellspacing="1">
<tr>
<td>Email:</td>
<td><input name="ctl00$MainContent$containerMain$loginMain$UserName" type="text" id="ctl00_MainContent_containerMain_loginMain_UserName" style="width:225px;" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="ctl00$MainContent$containerMain$loginMain$Password" type="password" id="ctl00_MainContent_containerMain_loginMain_Password" style="width:225px;" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="ctl00$MainContent$containerMain$loginMain$btnLogin" value=" Log In " id="ctl00_MainContent_containerMain_loginMain_btnLogin" class="ActionButton" />
</td>
</tr>
</table>
___________________________________________________________________________________________________________________________________
Thank you for any assistance!
I have a challenging VBA macro that attempts to Login to an intranet website. Using the code below (credit to a previous poster), I am able to navigate to the login webpage and populate the Username and Password fields, however, I cannot get the Submit portion to function. I have attempted previous forum examples without success. Through hours upon hours of trial and error, I am left stumped, as well as my team of IT professionals at my company. The problem seems to be the Form ID's not being a simple Username and Password, but not sure. Any advice or assistance would be greatly appreciated!!!
Below the VBA code below I pasted the View Source Website information.
'Needs references to Microsoft HTML Object Library and Microsoft Internet Controls
Option Explicit
Sub Test()
Const cURL = "https://mywebsite¬.com/login.aspx"
Const cUsername = "XXXX" 'REPLACE XXXX WITH YOUR USER NAME
Const cPassword = "YYYY" 'REPLACE YYYY WITH YOUR PASSWORD
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
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="ctl00$ct$UserName" type="text" maxlength="30" id="ctl00_ct_UserName" style="width:160px;" />
Set UserNameInputBox = LoginForm.elements("ctl00_MainContent_containerMain_loginMain_UserName ")
UserNameInputBox.Value = cUsername
'Get the password textbox and populate it
'< input name="ctl00$ct$Password" type="password" maxlength="30" id="ctl00_ct_Password" style="width:160px;" />
Set PasswordInputBox = LoginForm.elements("ctl00_MainContent_containerMain_loginMain_Password ")
PasswordInputBox.Value = cPassword
'Get the form input button and click it
'< input type="submit" name="ctl00$ct$uxBtnLogin" value="Sign In" o n c l i c k="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00_MainContent_containerMain_loginMain_btnLogin ", "", true, "Login", "", false, false))" id="ctl00_ct_uxBtnLogin" />
Set SignInButton = LoginForm.elements("ctl00$ct$uxBtnLogin")
SignInButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.busy: DoEvents: Loop
'Get the HTML document of the new page
Set doc = IE.document
'Determine whether login succeeded or not
If InStr(doc.body.innerText, "Invalid Login Information") = 0 Then
MsgBox "Login succeeded"
Else
MsgBox "Login failed"
End If
Debug.Print "Current URL: " & IE.LocationURL
End Sub
________________________________________________ WEBSITE HTML VIEW SOURCE________________________
Log In Required
</div>
<div class="LoginBox">
Please log in to access your Report-IT! site.
<div> </div>
<table border="0" cellpadding="4" cellspacing="1">
<tr>
<td>Email:</td>
<td><input name="ctl00$MainContent$containerMain$loginMain$UserName" type="text" id="ctl00_MainContent_containerMain_loginMain_UserName" style="width:225px;" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="ctl00$MainContent$containerMain$loginMain$Password" type="password" id="ctl00_MainContent_containerMain_loginMain_Password" style="width:225px;" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="ctl00$MainContent$containerMain$loginMain$btnLogin" value=" Log In " id="ctl00_MainContent_containerMain_loginMain_btnLogin" class="ActionButton" />
</td>
</tr>
</table>
___________________________________________________________________________________________________________________________________
Thank you for any assistance!