vba - 如何用VBA读取IE表格文本?

标签 vba excel internet-explorer

我正在尝试编写一个 vba 代码以遵循此过程:

  1. 自动填写并提交 Web 表单(打开一个新网页并显示答案 http://ec.europa.eu/taxation_customs/vies/?locale=en)

  2. 找到新网页的地址(因为我需要阅读这个内容)

  3. 读取html表格的特定单元格(公司名称)

您可以尝试使用增值税号 FR(FRANCE) 和 27435044714 手动提交申请。 它将返回一个包含公司名称的页面。

基本上我很难提取公司名称。

第 1 步运行良好,独立于第 2 步和第 3 步。 第 2 步和第 3 步在同一个 Sub 上:

宏从第2步开始(找到打开的网页)

Sub recup()
Dim SWs As SHDocVw.ShellWindows, IE As SHDocVw.InternetExplorer 
'Establish link to IE application
Set SWs = New SHDocVw.ShellWindows

For Each IE In SWs
    If Left(IE.LocationURL, 4) <> "http" Then
    GoTo autre
    End If
    Address = IE.LocationURL
    GoTo vabene 'avoid explorer windows/etc this way
autre:
Next
vabene:

然后,我继续第 3 步以提取文本。

IE.Visible = True
Dim xobj
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
Set xobj =IE.Document.getElementById("vatResponseFormTable").getElementsByClassName("labelStyle").Item(3)
Set xobj = xobj.getElementsByTagName("td").Item(0)
result = xobj.innerText
Set xobj = Nothing
IE.Quit
Set IE = Nothing
End Sub

我的问题:宏在线上停止(运行时错误 91):

result = xobj.innerText

这似乎来自上一行

Set xobj = xobj.getElementsByTagName("td").Item(0)

我在网上和这个论坛上搜索了很多(直到这一步它对我有帮助)。如果你能帮助我,那将节省我一周的时间!

最佳答案

试试这个

Sub getData()

'~~~~Variable declaration~~~~'
    Dim IE As Object
    Dim country As Object
    Dim num As Object
    Dim btn As Object
    Dim tlb As Object, td As Object

    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = False
    IE.navigate "http://ec.europa.eu/taxation_customs/vies/?locale=en"

'Wait till page is loaded
    Do While IE.readystate <> 4
        DoEvents
    Loop


    Set country = IE.document.getelementbyid("countryCombobox")
    country.Value = "FR" 'set the value for Member state


'Pause the code for 5 sec
    Application.Wait Now + TimeSerial(0, 0, 5)

'
    Set num = IE.document.getelementbyid("number")
    num.Value = "27435044714" 'set the Vat number


    Application.Wait Now + TimeSerial(0, 0, 5)


    Set btn = IE.document.getelementbyid("submit")
    btn.Click ' click the verify button

'Wait till page is loaded
    Do While IE.readystate <> 4: DoEvents: Loop

'Pause the code for 5 sec
        Application.Wait Now + TimeSerial(0, 0, 10)

        Set tbl = IE.document.getelementbyid("vatResponseFormTable")

        For Each td In tbl.getelementsbytagname("td")
            If td.innerText = "Name" Then
                MsgBox "Name : " & td.NextSibling.innerText
            ElseIf td.innerText = "Address" Then
                MsgBox "Address : " & td.NextSibling.innerText
            ElseIf td.innerText = "Consultation Number" Then
                MsgBox "Consultation Number : " & td.NextSibling.innerText
            End If

        Next


        IE.Quit
        Set IE = Nothing
 End Sub

关于vba - 如何用VBA读取IE表格文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21688478/

相关文章:

excel - 增加包含数字和文本 VBA 的单元格值

html - 消失的 HTML dingbat 实体

javascript - IE 不会在使用 window.open 创建的窗口中加载 PDF

c# - Excel公式T.INV计算错误使用C#

jQuery、$(element).click 在 ie<9 上不起作用

vba - 如何使用变量名称引用工作表上的控件对象?

vba - 运行时错误 '13' : Type mismatch defining PivotCahe

javascript - 从 VBA 调用 JavaScript 文件

python - 有没有办法在pywin32中解码数字COM错误代码

excel - 通过 VBA 创建数据透视表时出错