繁体   English   中英

Excel VBA代码从IE中的远程网站上的表单获取信息,然后在工作表上打印该信息不起作用?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM