Stevinho
Stevinho

Reputation: 33

GetElementById in VBA

I have a problem with entering data from Excel into a web browser using the code I found on the net and customized it to my needs. He reports error number 424 to me and I don't know how to solve it. Is it possible that there is a blockage on this site during automatic data entry? This is the code (inspect) from the site: input id="input_form1:j_idt26" name="input_form1:j_idt26" type="text" class="form-control searchbox"

Sub SearchBot()
    
    Dim objIE As InternetExplorer 'special object variable representing the IE browser
    Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
    Dim y As Integer 'integer variable we'll use as a counter
    Dim result As String 'string variable that will hold our result link
 
    'initiating a new instance of Internet Explorer and asigning it to objIE
    Set objIE = New InternetExplorer
 
    'make IE browser visible (False would allow IE to run in the background)
    objIE.Visible = True
 
    'navigate IE to this web page (a pretty neat search engine really)
    objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/registar-menica/"
 
    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
 
    'in the search box put cell "A2" value, the word "in" and cell "C1" value
    objIE.document.getElementById("input_form1:j_idt26").Value = _
      Sheets("Sheet1").Range("A2").Value
    objIE.document.getElementById("input_form1:j_idt21").Value = _
      Sheets("Sheet1").Range("A3").Value

Upvotes: 1

Views: 2164

Answers (1)

Raymond Wu
Raymond Wu

Reputation: 3387

The elements you are looking for is hiding in an iframe so it's abit tricky but this should work:

Sub SearchBot()
    
    Dim objIE As InternetExplorer 'special object variable representing the IE browser
    Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
    Dim y As Integer 'integer variable we'll use as a counter
    Dim result As String 'string variable that will hold our result link
 
    'initiating a new instance of Internet Explorer and asigning it to objIE
    Set objIE = New InternetExplorer
 
    'make IE browser visible (False would allow IE to run in the background)
    objIE.Visible = True
 
    'navigate IE to this web page (a pretty neat search engine really)
    objIE.navigate "https://nbs.rs/sr_RS/drugi-nivo-navigacije/servisi/registar-menica"
        
    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
 
    Dim ieDoc As MSHTML.HTMLDocument
    Set ieDoc = objIE.Document
    
    Dim iframeDoc As MSHTML.HTMLDocument
    Set iframeDoc = ieDoc.frames(0).Document
    
    'in the search box put cell "A2" value, the word "in" and cell "C1" value
    iframeDoc.getElementById("input_form1:j_idt26").Value = _
      Sheets("Sheet1").Range("A2").Value
    iframeDoc.getElementById("input_form1:j_idt21").Value = _
      Sheets("Sheet1").Range("A3").Value
End Sub

Upvotes: 1

Related Questions