You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel 2016中VBA调用ASMX服务:SoapClient30缺失的替代方案

替代SoapClient30调用ASMX服务的可行方案

由于SoapClient30依赖的MSSOAP30.dll属于旧版组件,Excel 2016默认不预装,最稳妥的替代方案是直接构造SOAP请求体,用Excel原生支持的MSXML2.XMLHTTP对象发送POST请求,无需额外部署任何DLL,且完全兼容原ASMX服务。

具体实现代码

替换原有的SoapClient初始化和调用逻辑,直接修改Add2DB_WebService过程如下:

Sub Add2DB_WebService()
    Dim xmlHttp As Object
    Dim soapEnvelope As String
    Dim responseXml As Object
    Dim strResp As String
    
    ' 初始化原生XMLHTTP对象(Excel 2016默认支持MSXML2.6.0)
    Set xmlHttp = CreateObject("MSXML2.XMLHTTP.6.0")
    
    ' 构造SOAP请求体,需根据原WSDL调整命名空间和参数节点
    soapEnvelope = "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & _
                   "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " & _
                   "xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & _
                   "<soap:Body>" & _
                   "<upload xmlns=""http://tempuri.org/"">" ' 替换为原WSDL中的目标命名空间
                   "<inputParameter1>" & inputParameter1 & "</inputParameter1>" & _
                   "<inputParameter2>" & inputParameter2 & "</inputParameter2>" & _
                   "<inputParameter3>" & inputParameter3 & "</inputParameter3>" & _
                   "<inputParameter4>" & inputParameter4 & "</inputParameter4>" & _
                   "</upload>" & _
                   "</soap:Body>" & _
                   "</soap:Envelope>"
    
    ' 发送POST请求到ASMX服务端点(注意不要带?WSDL后缀)
    xmlHttp.Open "POST", "http://blabla.com/ServiceName/Import.asmx", False
    xmlHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
    xmlHttp.setRequestHeader "SOAPAction", "http://tempuri.org/upload" ' 替换为原WSDL中upload方法的SOAPAction值
    
    ' 提交请求
    xmlHttp.send soapEnvelope
    
    ' 处理响应
    If xmlHttp.Status = 200 Then
        ' 解析返回的SOAP响应,提取结果
        Set responseXml = CreateObject("MSXML2.DOMDocument.6.0")
        responseXml.LoadXML xmlHttp.responseText
        ' 替换为实际的响应节点路径(从WSDL或原响应中确认)
        strResp = responseXml.SelectSingleNode("//uploadResult").Text
        MsgBox "上传完成:" & strResp
    Else
        MsgBox "请求失败,状态码:" & xmlHttp.Status & vbCrLf & "错误信息:" & xmlHttp.statusText
    End If
    
    ' 释放资源
    Set xmlHttp = Nothing
    Set responseXml = Nothing
End Sub

关键注意事项

  1. 命名空间与SOAPAction:
    打开原ASMX的WSDL地址(http://blabla.com/ServiceName/Import.asmx?WSDL),找到<wsdl:operation name="upload">节点,复制soap:operation属性中的soapAction值;同时确认<s:element name="upload">所在的命名空间,替换代码中对应的部分。
  2. 参数节点:
    代码中的<inputParameter1>等节点名称必须与原ASMX服务upload方法的参数名完全一致(大小写敏感)。
  3. 请求地址:
    使用ASMX服务的直接地址(不带?WSDL后缀),否则会请求失败。
  4. 特殊参数处理:
    如果参数包含特殊字符(如<、>、&),需要先做XML转义,比如用Replace函数替换为&lt;、&gt;、&amp;。

内容的提问来源于stack exchange,提问作者hakohako

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 03:11:18