我正在尝试编写一个 vba 代码以遵循此过程:
自动填写并提交 Web 表单(打开一个新网页并显示答案 http://ec.europa.eu/taxation_customs/vies/?locale=en)
找到新网页的地址(因为我需要阅读这个内容)
读取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/