【问题标题】:converting Excel VBA from InternetExplorerMedium to MSXML2.XMLHTTP60将 Excel VBA 从 InternetExplorerMedium 转换为 MSXML2.XMLHTTP60
【发布时间】:2018-12-19 21:15:55
【问题描述】:

我使用 InternetExplorerMedium 创建了一个宏,但是由于 IE 不稳定并且临时抛出错误,我想将其更改为使用 MSXML2.XMLHTTP60

到目前为止,我已经成功地更改了代码的特定部分,但是我遇到了我正在苦苦挣扎的部分。这部分代码但是我在runtime error 91处收到错误:

Set HTMLdoc = frame.contentDocument ' <----- error debug here

我该如何克服这个问题?

以下与此查询相关的代码片段:

Sub TPMRebatePayment()


    Dim IE As New MSXML2.XMLHTTP60
    Dim HTMLdoc As MSHTML.HTMLDocument
    Dim frame As HTMLFrameElement
    Dim myurl As String


<snip code>

    'Opens IE

    myurl = "http://crmprdas02.aunz.lncorp.net:8011/sap(bD1lbiZjPTEwMCZkPW1pbg==)/bc/bsp/sap/crm_bsp_frame/entrypoint.do?appl=crmd_stlmt_rb&version=0&blview=znfl_stl&crm_bsp_restore=false"
    IE.Open "GET", myurl, False
    IE.Send


    While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend

    'Loops thru entering payments
    LastRow = SourceShtTPM.Range("A" & Rows.Count).End(xlUp).Row    'Recalc last row as data has been entered

    For iRow = 3 To LastRow
        If SourceShtTPM.Range("A" & iRow) <> "" Then
            Set HTMLdoc = New HTMLDocument
            Set frame = HTMLdoc.getElementsByName("crmA")(0)

            ''' This is where the error occurs                
            Set HTMLdoc = frame.contentDocument

            HTMLdoc.getElementById("SREQ1_SR__simpleSearch__as_button").Click   'Click Search Button
            While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend

            HTMLdoc.getElementById("SREQ1_SR__advancedSearch_advancedSearch_REBATE_NO").Value = SourceShtTPM.Range("A" & iRow).Value    'Enter Accrual into Rebate No. Field
            HTMLdoc.getElementById("SREQ1_SR__advancedSearch__sm_go").Click
            While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend

            HTMLdoc.getElementById("SRES2_BUT_GOTO").Click      'Click Go To Button
            While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
            HTMLdoc.getElementById("EDIT_DETAILS").Click        'Then Details to enter the payment page
            While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend

            AccBal = HTMLdoc.getElementById("MULT3_DETL31_MULT3_DETL31ES_ZZACCRUED_SC").Value       'Scrapes accrual balance

            If AccBal <> 0 Then
                If Right(AccBal, 1) = "-" Then                                                          'Converts to number
                    SourceShtTPM.Range("E" & iRow).Value = "-" & Left(AccBal, Len(AccBal) - 1)
                    Else: SourceShtTPM.Range("E" & iRow).Value = "-" & AccBal
                End If

                If SourceShtTPM.Range("H" & iRow).Value > 0 Then       'Confirms if enough money to pay
                    HTMLdoc.getElementById("MULT3_DETL31_MULT3_DETL31ES_ZZAMOUNT").Value = Round(SourceShtTPM.Range("H" & iRow).Value, 2)   'Enters "Amount to be Paid"
                    HTMLdoc.getElementById("MULT3_DETL31_MULT3_DETL31ES_ZZCLAIMNO_SC").Value = SourceShtCLM.Range("A2").Value       'Enters claim no.
                    HTMLdoc.getElementById("MULT3_MEDL32_BUT_ZST_CPY_RT").Click     'Click button to distribute
                    While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
                    HTMLdoc.getElementById("ZCR_COPY_TO_SKU_RATE").Click            'distributes to sku
                    While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
                    HTMLdoc.getElementById("MULT3_MEDL32_BUT_ZSTL_COPY").Click      'Click button to distribute
                    While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
                    HTMLdoc.getElementById("ZCR_COPY_TO_SKU_AMNT").Click            'distributes to sku
                    While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
                    HTMLdoc.getElementById("MULT3_MEDL32_ZSTL_PART_SETTLE").Click   'Clicks Pay Claim
                    While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
                    'The line below will save the rebate payment.
                    'DO NOT CHANGE UNLESS CODE IS 100%
                    'HTMLdoc.getElementById("MULT3_MEDL32_ZCR_STLMT_SAVE").Click    'Clicks Save
                    'While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend

                    SourceShtTPM.Range("C" & iRow) = Split(IE.document.getElementsByName("crmA")(0).contentDocument.getElementById("APLG0_lnk").innerText, Chr$(32))(3)

                    'Col "Y" = entered commentary
                    SourceShtTPM.Range("D" & iRow).Value = "Claim Paid"
                Else
                    'Col "Y" = payment amount to enter
                    SourceShtTPM.Range("D" & iRow).Value = "Not Paid"
                End If
            Else
                SourceShtTPM.Range("D" & iRow).Value = "No money in accrual"
            End If

        IE.navigate "http://crmprdas02.aunz.lncorp.net:8011/sap(bD1lbiZjPTEwMCZkPW1pbg==)/bc/bsp/sap/crm_bsp_frame/entrypoint.do?appl=crmd_stlmt_rb&version=0&blview=znfl_stl&crm_bsp_restore=false"
        While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
        Set HTMLdoc = Nothing

        End If

    Next iRow


   IE.Quit

   <snip code>

End Sub

【问题讨论】:

  • 您创建 HTMLdoc 然后从中读取,但它没有内容并且是空的。该错误很可能是因为 frame 是 Nothing。您需要将下载的 HTML 文本分配给文档,例如 HTMLdoc.body.innerHTML = htmlText
  • 是的,你是对的,我将鼠标悬停在frame 上并且是= nothing。如何以及在何处将HTMLdoc.body.innerHTML = htmlText 合并到代码中?
  • 我在Set HTMLdoc = New HTMLDocument之后使用了HTMLdoc.Body.innerText = IE.responseText,debug.print输出页面
  • 该链接在我的浏览器中不起作用。它看起来是内部的。
  • 嗨@qharr 是的,它的内部......我会尽快测试并恢复

标签: html excel vba web-scraping


【解决方案1】:

您可以删除 while wend 循环。将 XMLHTTP 响应读入 html 文档对象。我通常在途中解码。

Dim sresponse As String, html As HTMLDocument, myUrl
myUrl = "abc.com"

With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", myUrl, False
    .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    .send
   sresponse = StrConv(.responseBody, vbUnicode)
End With

Set html = New HTMLDocument
html.body.innerHTML = sresponse

然后测试内容以查看框架是否存在并包含信息。

【讨论】:

  • html.Body.innerHTML = sresponse Run-time error '601' Application-defined or object-defined error 的错误
  • 你检查过响应吗?
  • Debug.Print sresponse 产生一些文本.. 有 一些文本
  • 为迟到的'tick'道歉 我遇到的问题是 CRM 和我升级后的 SAP 不兼容,因此这段代码不起作用。在理解了这一点并测试了上述内容之后,它就可以工作了。感谢您的帮助,再次抱歉耽搁了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-10
相关资源
最近更新 更多