0

我是 VBA 新手。我在 Microsoft Excel 2007 中编写了一个 VBA 代码来发送 SOAP 消息并处理soap 响应。我正在读取 Soap 响应并将值循环写入 Excel 单元格。示例肥皂响应是:-

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:getMyResponse xmlns:ns2="http://my.samplenamesapce/">
        <return>
            <col1>col1Value</col1>
            <col2>col2Value</col2>
            <col3>3.283E7</col3>
        </return>
        <return>
            <col1>col1Value</col1>
            <col2>col2Value</col2>            
        </return>       
      </ns2:getMyResponse>
   </soap:Body>
</soap:Envelope>

在单元格中写入响应的 VBA 代码是:

Private Sub getReportBtn_Click()
Dim WS As Worksheet: Set WS = Worksheets("my_report")
Dim request As New MSXML2.XMLHTTP60
Dim url As String
Dim response As New MSXML2.DOMDocument60
Dim requestData As String
    'Clear the Report Sheet
    WS.Range("A2:C65536").ClearContents
    url = Range("url").Value
    'Construct SOAP REQUEST
    request.Open "POST", url, False
    request.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""
    request.setRequestHeader "SOAPAction", ""
    'Create SOAP REQUEST BODY
    requestData = xmlBody
    request.setRequestHeader "Content-Length", Len(requestData)
    On Error GoTo err_handler:

    'Send SOAP REQUEST  
    request.send requestData
    'Read SOAP RESPONSE
    response.LoadXML request.responseText

    Dim MyReport As MSXML2.IXMLDOMNode
    Dim MyReportList As MSXML2.IXMLDOMNodeList
    Set MyReportList = response.getElementsByTagName("return")
    Dim i As Integer
    i = 1
    For Each MyReport In MyReportList
        i = i + 1
        WS.Range("col1Range").Cells(i).Value =  MyReport.selectSingleNode("col1").Text
        WS.Range("col2Range").Cells(i).Value =  MyReport.selectSingleNode("col2").Text
        If MyReport.SelectNodes("col3").Length > 0 Then
        WS.Range("col3Range").Cells(i).Value =  MyReport.selectSingleNode("col3").Text
        End If
    Next MyReport
    If i = 1 Then
        MsgBox "No data found for requested query!"
        Sheets("query").Select
        Exit Sub
    End If   
Sheets("my_report").Select
Exit Sub

err_handler:
    MsgBox "Error occurred during submission, please check your settings."
Exit Sub
End Sub

当我在 Windows 7(64 位)上运行我的 excel 表时,与在 Windows XP(32 位)上运行它相比,处理相同的数据需要双倍的时间。除了操作系统之外,两台机器的配置和物理位置都是相同的。

4

1 回答 1

0

您可能需要将变量声明为 32 位变量。要查看变量声明是否是延迟区域,请在代码的开头和结尾设置计时器,并在 WS.range clearContents 之前设置第三个计时器。我的预感是在 64 位机器上分配内存空间需要两倍的时间。

于 2013-07-22T19:50:59.810 回答