Share via

vba code stalls when website redirects

Anonymous
2015-12-27T20:08:48+00:00

Hello all

My overall goal seems relatively straightforward - to develop VBA code that gets data from a website.

The specific problem I’m facing arises from the fact that I have to logon to one website (let’s call this page URL-1) and then enter a password on a second, different, website (let’s call this one URL-2).

The navigation from URL-1 to URL-2 (from the logon page to the password page) occurs automatically after clicking a submit button on URL-1. Finally a button click on the password page (URL-2) finally gets me to the data tables.

My specific glitch is that the code stalls on URL #2 where the cursor just sits blinking in the empty password field.

Because my VBA skill level is modest at best, I can only guess at the cause of my problem.

Maybe -  the problem starts with the fact that code (see below) is associated URL #1 so that when the Internet Explorer is redirected to URL #2 the code becomes incorrect and will not run.  But right now I’m just guessing at that.

I've been working this issue for more than a week and my www searches have not been, directly, useful.

Incidentally, I've tried to solve this problem by stringing two subs (with different URL specifications) together but I can’t get that to work either.

Any help in developing code that navigates across URL-1 and -2 is appreciated

BobWald


Option Explicit

Public Sub CU_Login_Website()

Dim oBrowser As InternetExplorer

Dim HTMLDoc As HTMLDocument

Dim oHTML_Element As IHTMLElement

Dim sURL As String

    On Error GoTo Err_Clear

    sURL = https://www.URL-1.com    ‘this URL (URL-1) is the logon page - URL-2 (the password page) is not indicated,

Set oBrowser = New InternetExplorer

        oBrowser.Silent = True

        oBrowser.timeout = 60

        oBrowser.navigate sURL     'this is the logon webpage (URL-1)

        oBrowser.Visible = True

Do

' Wait till the Browser is loaded

    Loop Until oBrowser.readyState = READYSTATE_COMPLETE

    Set HTMLDoc = oBrowser.Document

        HTMLDoc.all.Login.Value = "########"      'submitted on URL-1 (login page)

        HTMLDoc.all.Password.Value = "aaaaaaaaa"   'submitted on URL-2 (password page)

    For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")

    If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For           ' this click appears to elicit the navigation from  URL-1 to URL-2

Next

' oBrowser.Refresh ' Refresh If Needed

Err_Clear:

If Err <> 0 Then

'Debug.Assert Err = 0

Err.Clear

Resume Next

End If

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2015-12-28T03:27:59+00:00

Just guessing (can test if you can share test URLs with id/pw)

You need to submit URL-1 then wait for next page to load, fill password and submit it and again wait till the page you want loads

Logic should go something like

HTMLDoc.all.Login.Value = "########"      'submitted on URL-1 (login page)

' Add code to click Submit button here on id page

<code to be added>

' Wait till page loads

Loop Until oBrowser.readyState = READYSTATE_COMPLETE

' Point to the new document

Set HTMLDoc = oBrowser.Document

' Fill password field

HTMLDoc.all.Password.Value = "aaaaaaaaa"   'submitted on URL-2 (password page)

' Add code to click Submit button here on password page

<code to be added>

' Wait till page loads

Loop Until oBrowser.readyState = READYSTATE_COMPLETE

' Point to the new document

Set HTMLDoc = oBrowser.Document

' DO what you want

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful