Hi, I have the following function I have written that appears to work on step in, but does not when running normally. It is supposed to load a webpage, login, navigate to the report webpage, enter the date range per cells in my spreadsheet, and then export the resulting data (Ive not written the export portion yet). Part of the issue is possibly waiting for the ready state, but I have tried various combinations of set delays and loops until ready with no good results.
Code:
Sub RenownDataPull()
Const cURL = "http://affiliate.renownholdings.com/Home.aspx" 'Enter the web address here
Dim Username As String
Dim Password As String
Username = "username" 'Enter your user name here
Password = "password"
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim ReportForm As HTMLFormElement
Dim UserNameInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim SignInButton As HTMLInputButtonElement
Dim ReportButton As HTMLInputButtonElement
Dim HTMLelement As IHTMLElement
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate cURL
'Wait for initial page to load
While IE.Busy
DoEvents
Wend
Set doc = IE.document
'Get the only form on the page
Set LoginForm = doc.forms(0)
While IE.Busy
DoEvents
Wend
'Get the User Name textbox and populate it
Set UserNameInputBox = LoginForm.elements("ctl00$ContentPlaceHolder1$lcLogin$txtUserName")
UserNameInputBox.Value = Username
'Get the password textbox and populate it
Set PasswordInputBox = LoginForm.elements("ctl00$ContentPlaceHolder1$lcLogin$txtPassword")
PasswordInputBox.Value = Password
'Get the form input button and click it
'Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
While IE.Busy
DoEvents
Wend
Set SignInButton = IE.document.getElementById("ctl00_ContentPlaceHolder1_lcLogin_btnSubmit")
SignInButton.Click
'Wait for the new page to load
Application.Wait (Now + #12:00:03 AM#)
IE.navigate "http://affiliate.renownholdings.com/RptCampaignPerformance.aspx"
While IE.Busy
DoEvents
Wend
Set ReportForm = doc.forms(0)
'assigning the input variables to the html elements of the form
IE.document.getElementsByName("ctl00$ContentPlaceHolder1$DateRange$ctl00$ctl00").Item.Value = Format(Range("DATE.START").Value, "mm/dd/yyyy")
IE.document.getElementsByName("ctl00$ContentPlaceHolder1$DateRange$ctl01$ctl00").Item.Value = Format(Range("DATE.END").Value, "mm/dd/yyyy")
Set ReportButton = IE.document.getElementById("ctl00_ContentPlaceHolder1_DateRange_refreshButton")
ReportButton.Click
While IE.Busy
DoEvents
Wend
IE.navigate "http://affiliate.renownholdings.com/Welcome/Logout.aspx"
While IE.Busy
DoEvents
Wend
IE.Quit
Set IE = Nothing
End Sub