Call web service in excel

前端 未结 4 1369
遥遥无期
遥遥无期 2020-12-08 04:59

In a VBA module in excel 2007, is it possible to call a web service? If so, any code snippets? How would I add the web reference?

相关标签:
4条回答
  • 2020-12-08 05:29

    Yes You Can!

    I worked on a project that did that (see comment). Unfortunately no code samples from that one, but googling revealed these:

    How you can integrate data from several Web services using Excel and VBA

    STEP BY STEP: Consuming Web Services through VBA (Excel or Word)

    VBA: Consume Soap Web Services

    0 讨论(0)
  • 2020-12-08 05:32

    In Microsoft Excel Office 2007 try installing "Web Service Reference Tool" plugin. And use the WSDL and add the web-services. And use following code in module to fetch the necessary data from the web-service.

    Sub Demo()
        Dim XDoc As MSXML2.DOMDocument
        Dim xEmpDetails As MSXML2.IXMLDOMNode
        Dim xParent As MSXML2.IXMLDOMNode
        Dim xChild As MSXML2.IXMLDOMNode
        Dim query As String
        Dim Col, Row As Integer
        Dim objWS As New clsws_GlobalWeather
    
        Set XDoc = New MSXML2.DOMDocument
        XDoc.async = False
        XDoc.validateOnParse = False
        query = objWS.wsm_GetCitiesByCountry("india")
    
        If Not XDoc.LoadXML(query) Then  'strXML is the string with XML'
            Err.Raise XDoc.parseError.ErrorCode, , XDoc.parseError.reason
        End If
        XDoc.LoadXML (query)
    
        Set xEmpDetails = XDoc.DocumentElement
        Set xParent = xEmpDetails.FirstChild
        Worksheets("Sheet3").Cells(1, 1).Value = "Country"
        Worksheets("Sheet3").Cells(1, 1).Interior.Color = RGB(65, 105, 225)
        Worksheets("Sheet3").Cells(1, 2).Value = "City"
        Worksheets("Sheet3").Cells(1, 2).Interior.Color = RGB(65, 105, 225)
        Row = 2
        Col = 1
        For Each xParent In xEmpDetails.ChildNodes
            For Each xChild In xParent.ChildNodes
                Worksheets("Sheet3").Cells(Row, Col).Value = xChild.Text
                Col = Col + 1
            Next xChild
            Row = Row + 1
            Col = 1
        Next xParent
    End Sub
    
    0 讨论(0)
  • 2020-12-08 05:49

    For an updated answer see this SO question:

    calling web service using VBA code in excel 2010

    Both threads should be merged though.

    0 讨论(0)
  • 2020-12-08 05:54

    Here's an overview from MS:

    Consuming Web Services in Excel 2007

    0 讨论(0)
提交回复
热议问题