Web Services - Microsoft Office Example
This example consists of the following:
-
A Web Service Class Module called clsws_WSTestService. This was created from the WSDL file
(http://localhost:8080/NorthgateWebServices/ws/WSTest?wsdl) using the Microsoft Office Web Services Toolkit (refer to the Microsoft Office Web Services Toolkit documentation for details of how to do this). This class has a method called wsm_GETADDRESS that calls the DataBasic subroutine GETADDRESS. -
A simple macro that calls the wsm_GETADDRESS method.
Web Service Class Module
Option Explicit
'*****************************************************************
'This class was created by the Microsoft Office 2003 Web Services Toolkit.
'
'Created: 9/28/2005 03:38:01 PM
'
'Description:
'This class is a Visual Basic for Applications class representation of the Web service
'as defined by http://localhost:8080/NorthgateWebServices/ws/WSTest?wsdl.
'
'To Use:
'Dimension a variable as new clsws_WSTestService, and then write code to
'use the methods provided by the class.
'Example:
' Dim ExampleVar as New clsws_WSTestService
' debug.print ExampleVar.wsm_GETADDRESS("Sample Input")
'
'For more information, see Complex Types in Microsoft Office 2003
'Web Services Toolkit Help.
'
'Changes to the code in this class may result in incorrect behavior.
'
'*****************************************************************
'Dimensioning private class variables.
Private sc_WSTestService As SoapClient30
Private Const c_WSDL_URL As String = _
"http://localhost:8080/NorthgateWebServices/ws/WSTest?wsdl"
Private Const c_SERVICE As String = "WSTestService"
Private Const c_PORT As String = "GETADDRESSPort"
Private Const c_SERVICE_NAMESPACE As String = _
"http://www.northgateps.com/proiv/webservices/wsdl"
Private Sub Class_Initialize()
'*****************************************************************
'This subroutine will be called each time the class is instantiated.
'Creates sc_ComplexTypes as new SoapClient30, and then
'initializes sc_ComplexTypes.mssoapinit2 with WSDL file found in
'http://localhost:8080/NorthgateWebServices/ws/WSTest?wsdl.
'*****************************************************************
Dim str_WSML As String
str_WSML = ""
Set sc_WSTestService = New SoapClient30
sc_WSTestService.MSSoapInit2 _
c_WSDL_URL, _
str_WSML, _
c_SERVICE, _
c_PORT, _
c_SERVICE_NAMESPACE
'Use the proxy server defined in Internet Explorer's LAN settings by
'setting ProxyServer to sc_WSTestService.ConnectorProperty("ProxyServer") = "<CURRENT_USER>"
'Autodetect proxy settings if Internet Explorer is set to autodetect
'by setting EnableAutoProxy to True
sc_WSTestService.ConnectorProperty("ProxyServer") = "<CURRENT_USER>"
sc_WSTestService.ConnectorProperty("EnableAutoProxy") = True
End Sub
Private Sub Class_Terminate()
'*****************************************************************
'This subroutine will be called each time the class is destructed.
'Sets sc_ComplexTypes to Nothing.
'*****************************************************************
'Error Trap
On Error GoTo Class_TerminateTrap
Set sc_WSTestService = Nothing
Exit Sub
Class_TerminateTrap:
WSTestServiceErrorHandler ("Class_Terminate")
End Sub
Private Sub WSTestServiceErrorHandler(str_Function As String)
'*****************************************************************
'This subroutine is the class error handler. It can be called from
'any class subroutine or function when that subroutine or function
'encounters an error. Then, it will raise the error along with the
'name of the calling subroutine or function.
'*****************************************************************
'SOAP Error
If sc_WSTestService.FaultCode <> "" Then
Err.Raise vbObjectError, str_Function, sc_WSTestService.FaultString
'Non SOAP Error
Else
Err.Raise Err.Number, str_Function, Err.Description
End If
End Sub
Public Sub wsm_GETADDRESS( _
ByRef str_REF As String, _
ByRef str_COMPANYNAME As String, _
ByRef str_ADDRESS As String, _
ByRef str_TELEPHONE As String, _
ByRef str_ZIP As String)
'*****************************************************************
'Proxy subroutine created from
'http://localhost:8080/NorthgateWebServices/ws/WSTest?wsdl.
'*****************************************************************
'Error Trap
On Error GoTo wsm_GETADDRESSTrap
sc_WSTestService.GetAddress _
str_REF, _
str_COMPANYNAME, _
str_ADDRESS, _
str_TELEPHONE, _
str_ZIP
Exit Sub
wsm_GETADDRESSTrap:
WSTestServiceErrorHandler "wsm_GETADDRESS"
End Sub
Example Macro
Option Explicit
Public Sub Test()
' Create an instance of the web service class.
Dim wsGetAddress As New clsws_WSTestService
' Declare the other variables we will need.
Dim str_REF As String
Dim str_COMPANYNAME As String
Dim str_ADDRESS As String
Dim str_TELEPHONE As String
Dim str_ZIP As String
str_REF = "HUNGC" ' Company reference.
wsGetAddress.wsm_GETADDRESS _
str_REF, _
str_COMPANYNAME, _
str_ADDRESS, _
str_TELEPHONE, _
str_ZIP
' Display the result.
Debug.Print str_COMPANYNAME
Debug.Print str_ADDRESS
Debug.Print str_TELEPHONE
Debug.Print str_ZIP
Debug.Print ' Blank line.
str_REF = "BOTTM" ' Company reference.
wsGetAddress.wsm_GETADDRESS _
str_REF, _
str_COMPANYNAME, _
str_ADDRESS, _
str_TELEPHONE, _
str_ZIP
' Display the result.
Debug.Print str_COMPANYNAME
Debug.Print str_ADDRESS
Debug.Print str_TELEPHONE
Debug.Print str_ZIP
End Sub
Result
When the example macro is run, the following is displayed in the Immediate window:
Hungry Coyote Import Store City Center Plaza 516 Main St.,Elgin,OR (503) 555-6874 97827 Bottom-Dollar Markets 23 Tsawassen Blvd.,Tsawassen,BC (604) 555-4729 T2F 8M4