1

I am trying to enter info into an Internet Explorer 11 website form. The website/form was designed a long time ago (around 15-20 years). The website can only be accessed through Internet Explorer.

I cannot share the website/source code as it is internal to my company.

I have browsed online for a solution, but none worked (I tried many different versions).

I am looking to login, then go through a few pages of entering information, while clicking next/submit at each stage. I am failing after I login.

I have the following references on Excel:

Microsoft Internet Controls, Microsoft HTML Object Library, Microsoft XML, v6.0

I am following the wise owl tutorial https://www.youtube.com/watch?v=dShR33CdlY8. Skip to about 17 mins in to see where I got the code.

I got an error message at the line htmlinput.Value = "excel".

The error message was

object variable or with block variable not set - Run Time error '91'

Sub navigate_website()

Dim ie As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim htmlinput As MSHTML.IHTMLElement

ie.Visible = True
ie.navigate Sheet1.Range("C2").Text

Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
    DoEvents
Loop

'enter in userid
ie.document.forms("formsamplename").elements("usedid").Value = ThisWorkbook.Sheets("sheet1").Range("B6")
'enter in password
ie.document.forms("formsamplename").elements("userpassword").Value = ThisWorkbook.Sheets("sheet1").Range("B7")
'click the login button
ie.document.forms("formsamplename").elements("cmdSubmit").Click

Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
    DoEvents
Loop

' ----- I tried the below code as an alternative but it didn't work -----
'ie.document.forms("formsamplename").elements("usernumber").Value = ThisWorkbook.Sheets("sheet1").Range("B6")

Set htmldoc = ie.document
Set htmlinput = htmldoc.getElementById("usernumber")

htmlinput.Value = "excel" **'error occurs here**

' ----- I also tried the below code, but it didn't work -----
'htmldoc.forms("formsamplename").elements("usernumber").Value = "test"

Set ie = Nothing
Set htmldoc = Nothing
Set htmlinput = Nothing

End Sub
21
  • What if you do Dim htmlDoc as Object // Set htmlDoc = CreateObject("InternetExplorer.application") // ... // Dim htmlPage as Object // Set htmlPage = htmlDoc.document // htmlPage.getElementById("usernumber").Value = "excel"?
    – BruceWayne
    Commented Jan 7, 2020 at 14:57
  • The second Do While ie.Busy... doesn't work because the browser status is already complete. Instead of that make a manual break for a few seconds to load the page after the click in the first part of code: Application.Wait (Now + TimeSerial(0, 0, 3))
    – Zwenn
    Commented Jan 7, 2020 at 15:07
  • @Zwenn, thanks for letting me know that the status is already complete when I had the second do while loop. I tried replacing the second do while ie.busy with the application wait, but unfortunately it still does not work. I tried up to 15 seconds and I still get the error message at htmlinput.Value = "excel" Commented Jan 7, 2020 at 15:20
  • @BruchWayne, would you mind telling me exactly where to put in the changes/replacements/deletions? I tried some but none worked (I am new to VBA). Appreciate the response! Commented Jan 7, 2020 at 15:27
  • Welcome to SO. What happens when you do Debug.Print htmlinput.ID instead of htmlinput.Value = "excel"? If it prints usernumber in Immediate Window it means that the object is properly loaded at that stage of executing the code.
    – Justyna MK
    Commented Jan 7, 2020 at 15:33

0

Browse other questions tagged or ask your own question.