Llame al servicio web en excel
-
19-08-2019 - |
Pregunta
En un módulo VBA en Excel 2007, ¿es posible llamar a un servicio web? Si es así, ¿algún fragmento de código? ¿Cómo agregaría la referencia web?
Solución
¡Sí puedes!
Trabajé en un proyecto que hizo eso (ver comentario). Desafortunadamente, no hay muestras de código de ese, pero Google reveló estos:
Cómo puede integrar datos de varios servicios web usando Excel y VBA
PASO A PASO: Consumir servicios web a través de VBA (Excel o Word)
Otros consejos
Aquí hay una descripción general de MS:
Para obtener una respuesta actualizada, vea esta pregunta SO:
llamando al servicio web utilizando el código VBA en Excel 2010
Sin embargo, ambos hilos deberían fusionarse.
En Microsoft Excel Office 2007 intente instalar " Herramienta de referencia de servicios web " enchufar. Y use el WSDL y agregue los servicios web. Y use el siguiente código en el módulo para obtener los datos necesarios del servicio web.
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