VBA Web Scraping Function works on step in, but not when ran in full?

CylonWolf

New Member
Joined
Jul 6, 2012
Messages
4
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
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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