【问题标题】:getElementsByName in VBAVBA 中的 getElementsByName
【发布时间】:2021-07-21 09:22:04
【问题描述】:

我在使用以下代码将 Excel 中的数据输入 Web 浏览器时遇到问题。他向我报告错误号 438,我不知道如何解决。 错误出现在这里:

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

这是我的完整代码:

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

我在网上搜索了解决方法,但无法修复错误。

我尝试设置循环,但没有任何条目。

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

【问题讨论】:

  • getElementsByName 返回该名称的元素集合,您需要循环集合并设置值。 w3schools.com/jsref/met_doc_getelementsbyname.asp
  • 我看到只有 1 个元素的名称属性为“matbr”,因此您可以尝试iframeDoc.getElementsByName("matbr")(0).Value = Range("a2").Value。只有 getElementById 返回单个元素,而其余的返回元素集合。
  • Raymond Wu - 我在这里写之前尝试过,但没有用。 Nathan_Sav - 我也尝试过,但目标字段中没有任何条目。
  • x 也是一个集合,所以x = "matbr" 不可能发生。 x(i) 可能发生。
  • @Nathan_Sav 我更改代码x(i),但仍然没有

标签: vba getelementsbyname


【解决方案1】:

我找到了解决办法,谢谢大家! 这是正确的代码:

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

【讨论】:

    猜你喜欢
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 2014-12-08
    • 1970-01-01
    • 2016-01-01
    • 2011-02-28
    • 2011-10-21
    相关资源
    最近更新 更多