我是VBA的新手。我在Microsoft Excel 2007中编写了一个VBA代码来发送SOAP消息并处理soap响应。 我正在阅读Soap Response并在循环中将值写入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位)上运行它以处理相同的数据相比,它花费了两倍的时间。 除操作系统外,两台机器的配置和物理位置相同。
答案 0 :(得分:0)
您可能需要将变量声明为32位变量。要查看变量声明是否是延迟区域,请在代码的开头和结尾设置定时器,在WS.range clearContents之前设置第三个定时器。我的预感是,在64位计算机上分配内存空间需要两倍的时间。