首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA中的getElementsByName

VBA中的getElementsByName
EN

Stack Overflow用户
提问于 2021-07-21 17:22:04
回答 1查看 86关注 0票数 0

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

代码语言:javascript
复制
iframeDoc.getElementsByName("matbr").Value = Range("a2").Value

这是我的完整代码:

代码语言:javascript
复制
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

我在网上寻找解决方案,但我不能修复这个错误。

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-07-21 20:38:44

我找到了一个解决方案,谢谢大家!下面是正确的代码:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68467034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档