【发布时间】:2018-02-07 15:42:34
【问题描述】:
我正在使用 excel 2013 和包含一列许可证编号的工作表。
宏需要循环遍历网页上要搜索的每个单元格。 搜索后,它会从搜索结果页面中获取一些详细信息,并将其放在许可证号旁边的单元格中。
我遇到的问题是,并不是所有的许可证号都可以找到,这导致网站抛出错误,而不是进入导致代码失败的结果页面。
我需要一种方法来跳过引发网页错误的许可证号并继续到下一个条目。
另一个更让人头疼的问题是,插入到单元格中的结果来自表格格式,因此它使单元格变得庞大并摆脱了工作簿格式。有没有办法从搜索结果中仅获取特定细节或确保将其置于“未包装”格式?
重新创建宏的详细信息。
该网站需要您创建一个免费的用户名和密码,但创建后应该保存一个 cookie,因此代码不包含登录部分。
网页搜索位置(登录后):https://aca3.accela.com/MILARA/GeneralProperty/PropertyLookUp.aspx?isLicensee=Y&TabName=APO
错误页面:https://aca3.accela.com/MILARA/Error.aspx?ErrorId=2d09a60802d2455b8fbf1b86e45033d2
Sub SearchPage()
Dim IE As Object
Dim search As Variant
Dim button As Variant
Dim LR As Integer
Dim var As String
Dim var1 As Object
Dim CurrentWindow As HTMLWindowProxy
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var = Cells(x, 1).Value
Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
With IE
.Visible = True
.navigate "https://aca3.accela.com/MILARA/GeneralProperty/PropertyLookUp.aspx?isLicensee=Y&TabName=APO"
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While IE.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
'this is where it puts the number from the cell into the search box
'below is the text search box and the cell containing the ID IE.document.getElementById("ctl00_PlaceHolderMain_refLicenseeSearchForm_txtLicenseNumber").Value = var
'Here we are clicking on search Button
IE.document.getElementById("ctl00_PlaceHolderMain_btnNewSearch").Click
'wait for page to load
Application.Wait (Now + TimeValue("0:00:02"))
While IE.Busy
DoEvents
Wend
'grabs results from search and places it in cell next to searched id
Set var1 = IE.document.getElementById("ctl00_PlaceHolderMain_upGeneralInfo")
Cells(x, 2).Value = var1.innerText
IE.Quit
Set IE = Nothing
Next x
End Sub
【问题讨论】:
-
我已经解决了如何从搜索结果表中识别和抓取特定实例,但是我仍然无法让宏跳过导致错误页面的搜索。
-
我不知道为什么这会被否决。我进行了研究,包含了代码,并尽我所能对目标进行了简明的描述。如果人们要诋毁我的问题,最好有一些关于原因的反馈。尤其是你的分数会影响你提问的能力。
标签: vba excel ie-automation