# Connect to web service



## danik69 (Nov 15, 2011)

Hi i having a problems trying to create some vba code to connect to a web service.

hopefully i will describe this as best i can.

I am using and have to develop this in Microsoft Excel 2003 using VB.

I have tried using the microsoft Web Services Toolkit but unfortunately it wont create the classes correctly so I have had to go down the route of hard coding it myself.

I am trying to connect to this web service

WebCrmApi Web Service

This is the class in my worksheet


```
'*****************************************************************
'This class was created by the Microsoft Office 2003 Web Services Toolkit.
'
'Created: 11/15/2011 11:07:48 AM
'
'Description:
'This class is a Visual Basic for Applications class representation of the Web service
'as defined by http://www.webservicex.net/stockquote.asmx?WSDL.
'
'To Use:
'Dimension a variable as new clsws_StockQuote, and then write code to
'use the methods provided by the class.
'Example:
' Dim ExampleVar as New clsws_StockQuote
' debug.print ExampleVar.wsm_GetQuote("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_Connect As SoapClient30
Private Const c_WSDL_URL As String = "https://b2b-email.net/apicrm1/webCRMAPI.asmx?WSDL"
Private Const c_SERVICE As String = "WebCrmApi"
Private Const c_PORT As String = "WebCrmApiSoap"
Private Const c_SERVICE_NAMESPACE As String = "http://www.webcrm.com/"

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://www.webservicex.net/stockquote.asmx?WSDL.
    '*****************************************************************

    Dim str_WSML As String
    str_WSML = ""

    Set sc_Connect = New SoapClient30

    sc_Connect.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 <CURRENT_USER>
    sc_Connect.ConnectorProperty("ProxyServer") = "<CURRENT_USER>"
    'Autodetect proxy settings if Internet Explorer is set to autodetect
    'by setting EnableAutoProxy to True
    sc_Connect.ConnectorProperty("EnableAutoProxy") = True

    
End Sub

Public Function wsm_Auth(ByVal dbnCode As String, user As String, pass As String) As String
    '*****************************************************************
    'Proxy function created from http://www.webservicex.net/stockquote.asmx?WSDL.
    '*****************************************************************

    'Error Trap
    On Error GoTo wsm_AuthTrap

    wsm_Auth = sc_Connect.Auth(dbnCode, user, pass)

Exit Function
wsm_AuthTrap:
    WebCrmErrorHandler "wsm_GetAuth"
End Function

Private Sub WebCrmErrorHandler(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_Connect.FaultCode <> "" Then
        Err.Raise vbObjectError, str_Function, sc_Connect.FaultString
    'Non SOAP Error
    Else
        Err.Raise Err.Number, str_Function, Err.Description
    End If

End Sub
```

This is code that runs when a button is clicked within the worksheet


```
Private Sub CommandButton1_Click()
 Dim ExampleVar As New clsws_StockQuote
 Print ExampleVar.wsm_Auth("test", "test", "test") 
End Sub
```
so i am trying to pass the parameters to the function Auth.

at the moment all it currently does is throw this error

"Run Time Error 438 Object Error Doesnt Support This Property Or Method!"

Am i on the write lines in terms of utilising the web service to authenticate? 
Anyone know why it would be throwing this error? 
Please explain in simple terms as i am not that experienced in VB Code.

Thanks for any help.


----------



## Corday (Mar 3, 2010)

I'm moving your thread to a place where more Visual Basic help should be available.


----------



## Ninjaboi (Dec 27, 2010)

Hello danik69, welcome to TSF!

This is from the Microsoft website on the matter:



> Run Time Error 438 - Object Doesn't Support this Property or Method
> The most common cause of error 438 is not maintaining binary compatibility between successive versions of your components. Each COM interface has an associated GUID that is called an interface ID (IID). Each coclass has an associated GUID that is called class ID (CLSID). When you compile an ActiveX component in Visual Basic, the CLSIDs and IIDs are compiled into the component's type library.
> Example
> A program that consists of a Visual Basic client and an ActiveX DLL is released to the user community. At a later time, additional functionality is to be added to the DLL component. The necessary modifications are made, and the ActiveX DLL is compiled without maintaining binary compatibility. When the DLL is released, the client that is trying to use the DLL will throw run time error 438. The reason this occurs is that when the DLL was compiled, a fresh set of GUIDs was compiled into the DLL, and the client has no reference to these new GUIDs. This is why it is important to maintain binary compatibility with the last-released version of the component when you are trying to release a newer version.
> ...


----------



## danik69 (Nov 15, 2011)

Thanks For Your Replys

I have managed to get past this error but have now encountered another problem.

Here i have my Authenticate Class


```
Public Function wsm_Authenticate(ByVal dbnCode As String, ByVal userName As String, ByVal password As String) As String
        
    wsm_Authenticate = sc_StockQuote.Authenticate(dbnCode, userName, password)
    
    End Function
```
Here i have my initialize part of my class


```
Private sc_StockQuote As SoapClient30
Private Const c_WSDL_URL As String = "https://b2b-email.net/apicrm1/webCRMAPI.asmx?WSDL"
Private Const c_SERVICE As String = "WebCrmApi"
Private Const c_PORT As String = "WebCrmApiSoap"
Private Const c_SERVICE_NAMESPACE As String = "http://www.webcrm.com/"

Private Sub Class_Initialize()
      

    Dim str_WSML As String
    str_WSML = ""

    Set sc_StockQuote = New SoapClient30

    sc_StockQuote.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 <CURRENT_USER>
    sc_StockQuote.ConnectorProperty("ProxyServer") = "<CURRENT_USER>"
    'Autodetect proxy settings if Internet Explorer is set to autodetect
    'by setting EnableAutoProxy to True
    sc_StockQuote.ConnectorProperty("EnableAutoProxy") = True
    
    MsgBox ("Connected Successfully to Web Service")
    
    
End Sub
```
So now in my worksheet i have a button. when its clicked it creates a new instance of the class and then try s to send the three parameters to authenticate function. 


```
Private Sub CommandButton1_Click()

Dim ExampleVar As New clsws_StockQuote

MsgBox (ExampleVar.wsm_Authenticate("test", "test", "Test"))

End Sub
```
However i just get an error 

"Run time error 450"

"Wrong Number Of arguments or Invalid Property Assignment"

Can anyone see what i am doing wrong?


----------



## Ninjaboi (Dec 27, 2010)

Hello again.

This is also from the Microsoft website on the new error:



> The number of arguments to a procedure must match the number of parameters in the procedure's definition. This error has the following causes and solutions:
> 
> The number of arguments in the call to the procedure wasn't the same as the number of required arguments expected by the procedure. Check the argument list in the call against the procedure declaration or definition.
> You specified an index for a control that isn't part of a control array. The index specification is interpreted as an argument but neither an index nor an argument is expected, so the error occurs. Remove the index specification, or follow the procedure for creating a control array. Set the *Index *property to a nonzero value in the control's property sheet or property window at design time.
> ...


Remember, MSDN was made to be a HUGE archieve of developer tools, solutions, troubleshooting, documentation, and much more. Utilizing it when you first encounter an error will save you time and hassle for sure.

Best of luck to you!


----------

