getElementsByName in VBA

Stevinho

New Member
Joined
Jul 21, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
I have a problem with entering data from Excel into a web browser using the code below. He reports error number 438 to me and I don't know how to solve it.
The error appeared here:

iframeDoc.getElementsByName("matbr").Value = Range("a2").Value

This is my full code:

VBA Code:
 Sub provera_menice()
    
    Dim wb As Workbook
    Set wb = ThisWorkbook
    
    'Dim rgMB As Range
    'Set rgMB = Application.InputBox("Izabrati MB klijenta koji se deblokira.", Type:=8)
    
    'Dim r As Long
    'Dim k As Long
    'r = rgMB.Row
    'k = rgMB.Column
    
        Dim objIE As InternetExplorer 'special object variable representing the IE browser
        Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
     
        '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/duznici-pn/"
            
        '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 some cell value
        'MB: <input class="form-control" name="matbr" type="text" tabindex="4" size="13" maxlength="8" value="">
        iframeDoc.getElementsByName("matbr").Value = Range("a2").Value
        
        'search: <input class="btn" type="submit" name="send" id="send" value="????????" tabindex="8">
        iframeDoc.getElementById("send").Click
        
    End Sub

I searched for a solution on the net, but I can't fix the error.

I try to set loop, but nothing entry.

VBA Code:
    Set x = iframeDoc.getElementsByName("matbr")
    For i = 0 To x.Length
        If x(i) = "matbr" Then
            'in the search box put some cell value
            'MB: <input class="form-control" name="matbr" type="text" tabindex="4" size="13" maxlength="8" value="">
            iframeDoc.getElementsByName("matbr").Value = Range("a2").Value
            'search: <input class="btn" type="submit" name="send" id="send" value="????????" tabindex="8">
            iframeDoc.getElementById("send").Click
        End If
        i = i + 1
    Next
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I found a solution!
Here is the correct code:

VBA Code:
 Sub provera_menice()
    
    Dim wb As Workbook
    Set wb = ThisWorkbook
    
    'Dim rgMB As Range
    'Set rgMB = Application.InputBox("Izabrati MB klijenta koji se deblokira.", Type:=8)
    
    'Dim r As Long
    'Dim k As Long
    'r = rgMB.Row
    'k = rgMB.Column
    
        Dim objIE As InternetExplorer 'special object variable representing the IE browser
        Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
     
        '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/duznici-pn/"
            
        '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

        iframeDoc.getElementsByName("matbr")(1).Value = Range("a2").Value
        iframeDoc.getElementById("send").Click

        Range("b4").Value = iframeDoc.getElementsByClassName("table_text cell")(5).textContent
     
    End Sub
 
Upvote 0
Try like this:
VBA Code:
        Dim frm As Variant
        Set frm = iframeDoc.getElementsByName("matbr")
        frm.Item(1).Value = Range("a2").Value
 
Upvote 0
@bobsan42 Thank you for your response, but I haven't tried your suggestion, because I found the solution I pin up above.
 
Upvote 0
Well it's basically the same thing.
You go a level deeper using the item's index.
But it's a good feeling to solve things yourself ?.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top