Get an element by name and set its value

225 views Asked by At
Sub FlashReportAutomation()
 
  Dim web As Object
  Dim doc As HTMLDocument
    
  Set web = CreateObject("internetexplorer.application")
  
  web.Visible = True
  web.navigate "http://google.com"
  
  Do While web.Busy
  Application.Wait DateAdd("s", 2, Now)
  Loop
  
  
Set doc = web.Document

web.Document.getElementsByName("q").Value = "Robert"

End Sub

I'm unable to get this working, if element ID is not mentioned. Any help?

1

There are 1 answers

0
Deepak-MSFT On

I agree with the suggestion given by Tim Williams regarding passing the index number for the element with the name 'Q'.

Another thing I noticed is that in the line below you have set the document object.

Set doc = web.Document

While assigning the value to the element, you are again using the document. It will generate an error.

web.Document.getElementsByName("q").Value = "Robert"

Your code should be like below.

Set doc = web.document

doc.getElementsByName("q")(0).Value = "Robert"

You could try like this and let us know if you have questions.