最近在做的一個功能,就是用excel表格通過IE物件實作在線轉換GPS坐標的功能,代碼如下
Private Sub test()
Dim str, t, t1, s1, s, l As String
Dim d, g, q, i As Long
Dim nowtime, clicktime, retime As Single
Dim ie As Object
i = 0
Set ie = CreateObject("InternetExplorer.application")
ie.Navigate "https://tool.lu/coordinate/" '訪問網站
ie.Visible = True '打開后顯示網頁
nowtime = Timer
Do Until ie.ReadyState = 4 And ie.busy = False '如果IE加載完成
DoEvents '在表單加載時轉交操作權
If Abs(Timer - nowtime) >= 10 Then
ie.qiut
MsgBox "超時"
Sheet1.Application.StatusBar = ""
Exit Sub
End If
Loop
With ie
.document.getelementbyid("wgs84").innertext = ""
.document.getelementbyid("src_coordinate").Value = Trim(Sheet1.Range("C1" ))
.document.all.tags("input")(2).Checked = "1"
.document.all.tags("button")(1).Click
clicktime = Timer '控制回圈間隔
Do Until .ReadyState = 4 And .busy = False And Timer > clicktime + 0.3 '如果IE加載完成
DoEvents '在表單加載時轉交操作權
If Abs(Timer - clicktime) >= 10 Then
ie.Quit
Set ie = Nothing
MsgBox "超時"
Sheet1.Application.StatusBar = ""
Exit Sub
End If
Loop
End With
End If
End Sub
這個代碼除錯狀態逐條運行沒一點問題,可就是直接連續運行的話,那個網站一打開,我能看到坐標系的那個單選框從最初的第三個蹦到第二個,說明.document.all.tags("input")(2).Checked = "1"成功執行了,但是不到0.1秒的時間就迅速自動跳到了第三個然后click了。。。。這樣我查出來的坐標就不對了。。。這問題該怎么解決?
而且奇怪的是如果我用回圈批量查詢多個資料時往往也是最開始的一兩個資料會有問題,到后面又都沒問題了。。。目前我的解決辦法簡單粗暴,就是在第一個DO回圈條件里加上timer>clicktime+2,就沒問題了,但是感覺這種方法太被動而且不方便。。。
各位大神有更好的解決方法嗎?
uj5u.com熱心網友回復:
我又百度了一下,目前感覺可能是iframe的問題,本來可以通過記錄DocumentComplete觸發的次數來判斷是否加載完畢,可是為啥我直接使用IE物件就無法觸發了?一定要使用wb控制元件嗎?轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/40407.html
標籤:VBA
