I am relatively new at VBA (I know the basics, but not much else) and I am trying to get some code set up that will fill out online forms for me, but when I run my code I get a 438 error:
object doesnt support this property or method
when it gets to
I have added the HTML object library and Microsoft internet controls to my references. I have looked at tons of online forums. I have even copied and pasted an entire script directly into VBA. Nothing will make it use
getElementById(). Here is my code:
Sub internetstuff() Dim ie As Object Set ie = CreateObject("internetexplorer.application") ie.Visible = True ie.navigate ("https://www.google.com/") Set searchbx = ie.document.getElementById("q") searchbx.Value = "Howdy!" End Sub
What should happen is that it should open InternetExplorer, go to Google, and populate the search bar with “Howdy!”.
Instead, It only opens google, then I get the error message and it stops running.
"q" is not an
ID, it’s a
You have to use
GetElementsByName and select the 1st Element
Sub internetstuff() Dim ie As Object Set ie = CreateObject("internetexplorer.application") ie.Visible = True ie.Navigate ("https://www.google.com/") Do Until ie.ReadyState >= 4 DoEvents Loop Set searchbx = ie.document.getElementsByName("q")(0) searchbx.Value = "Howdy!" End Sub
I have also added a waiting Event, in case it takes time to load Google.
A more concise and efficient way is to use querySelector to return first match for the name attribute
Option Explicit Public Sub internetstuff() Dim ie As Object Set ie = CreateObject("InternetExplorer.Application") With ie .Visible = True .Navigate2 "https://www.google.com/" While .Busy Or .readyState < 4: DoEvents: Wend .document.querySelector("[name=q]").Value = "Howdy" Stop '<delete me later .Quit End With End Sub