[英]excel vba code to get info from a form on a remote website in ie, and then print that info on worksheet isn't working?
你們能讀這個嗎,請嘗試我的代碼,看看它是否適合您? 它沒有在我的工作表上輸入數字,但對其他人有用。 我在Excel 2010中的VBA中的常規模塊(不是類模塊,工作表模塊等)中具有代碼。
首先,它應該打開IE並轉到下面的網頁。 然后,代碼應在活動工作表上的單元格A20中輸入數字2688(或其他一些4位數字)。
prodID是否應作為對象變暗? 它是否需要在頂部顯示明確的對象? 或使用其他類型的模塊? 我的設置可能有問題嗎? 還是為什么對我不起作用?
Sub work_damit()
Dim ieApp As Object
Dim URL As String
Dim prodID As Object
URL = "http://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx"
Set ieApp = CreateObject("InternetExplorer.Application")
With ieApp
.Navigate URL
.Visible = True
Label1:
Application.Wait (Now() + TimeValue("0:00:10"))
On Error GoTo errorHandler:
Set prodID = .document.getElementByID("ProductID")
Range("A20").Value = prodID.Value
.Quit
End With
Exit Sub
errorHandler:
If Err.Number <> 462 Then
GoTo Label1:
End If
End Sub
您所依賴的時間是網頁加載的時間,如果10秒鍾后仍未加載,則代碼會轉到錯誤處理程序。
更新的代碼
此版本使用xmlhttp
獲取數據
Public Sub SidsCode()
'http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26686033.html
Dim objIE As Object
Dim objxmlhttp As Object
Dim strURL
On Error GoTo errhandler
strURL = "http://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "about:blank"
Set objxmlhttp = CreateObject("Microsoft.xmlhttp")
With objxmlhttp
.Open "GET", strURL, False
objxmlhttp.setRequestHeader "Content-Type", "text/xml"
objxmlhttp.send
If .Status = 200 Then
objIE.document.write objxmlhttp.responseText
ActiveSheet.Range("A20").Value = objIE.document.getElementByID("ProductID").Value
Else
MsgBox "no reponse from site"
End If
End With
objIE.Quit
Set objIE = Nothing
Exit Sub
errhandler:
MsgBox "Code failed on" & vbNewLine & Err.Description
objIE.Quit
Set objIE = Nothing
End Sub
原始碼
相反,您可以這樣編碼頁面的readystate:
Sub work_damit()
Dim ieApp As Object
Dim URL As String
Dim prodID As Object
URL = "http://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx"
Set ieApp = CreateObject("InternetExplorer.Application")
With ieApp
.Navigate URL
Do While .readystate <> 4
DoEvents
Loop
Set prodID = .document.getElementByID("ProductID")
Range("A20").Value = prodID.Value
.Quit
End With
Set ie = Nothing
End Sub
它對我來說很完美。 我在想的是,那段時間您的瀏覽量會增加嗎? 如果沒有,您可能會得到一個錯誤。
而不是使用
Application.Wait (Now() + TimeValue("0:00:10"))
采用
Do While ieApp.Busy
DoEvents
Loop
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.