Winsock and vba Excel 
Author Message
 Winsock and vba Excel

Here is my problem : from an Excel sheet I want to establish a
connection to a distant host and get http source code.

To do this, I have an excel sheet with a reference to a DLL I wrote.
This DLL refers a Microsoft Winsock (see code at the end of mail).

In my Excel sheet I have a command button with the following code :

Private Sub CommandButton1_Click()
'------------------------------------------------------------------------------

'
'------------------------------------------------------------------------------

Dim TT          As wSocket

    'Create a new wSocket object
    Set TT = New wSocket
    'Initialize it
    TT.RemoteHost = "195.115.134.2"
    TT.RemotePort = "80"
    TT.DataToSend = "GET
http://www.*-*-*.com/ %3D33"

    'Connect
    TT.Connect
    'Wait while TT is not connected
    While Not TT.State = 7
        DoEvents
    Wend
    'Send data once connected
    TT.SendData
    'Wait while remote host is not closing connection
    While Not TT.State = 8
        DoEvents
    Wend
    'Data returned
    ActiveSheet.Cells(1, 1) = Trim$(TT.DataReceived)
    'Set TT to nothing
    Set TT = Nothing

End Sub

What's wrong ?

When I execute code step by step, everything goes right. But when I
click on my command button, execution loop on the second While Wend
part.
I think I have some trouble with event gestion trough Excel ?

PS : DLL source : 1 VB6 DLL project with only one class module :

Option Explicit
'Value returned by GetData
Private sDataReceived As String
'Data to send
Public sDataToSend As String
'sck respond to events triggered by Winsock ActiveX
Dim WithEvents sck As Winsock
'sck respond to DataArrival event with sck_DataArrival Sub
Public Event DataArrival(ByVal bytesTotal As Long)

Private Sub sck_DataArrival(ByVal bytesTotal As Long)
'--------------------------------------------------------
' Event DataArrival
'--------------------------------------------------------
Dim sTemp As String

    sTemp = String(bytesTotal, Chr$(0))

    sck.GetData sTemp, vbString

    sDataReceived = sDataReceived & sTemp

End Sub

Private Sub Class_Initialize()
'--------------------------------------------------------
' Initialize class
'--------------------------------------------------------

    Set sck = New Winsock
    sDataReceived = vbNullString
    sck.Protocol = sckTCPProtocol

End Sub

Public Property Let DataToSend(ByVal sData As Variant)
'--------------------------------------------------------
' Property Protocol
'--------------------------------------------------------

    sDataToSend = sData

End Property

Public Property Let RemotePort(ByVal sRemotePort As Variant)
'--------------------------------------------------------
' Property RemotePort
'--------------------------------------------------------

    sck.RemotePort = sRemotePort

End Property

Public Property Let RemoteHost(ByVal sRemoteHost As Variant)
'--------------------------------------------------------
' Property RemoteHost
'--------------------------------------------------------

    sck.RemoteHost = sRemoteHost

End Property

Public Property Get State() As Variant
'--------------------------------------------------------
' State of the Winsock
'--------------------------------------------------------

    State = sck.State

End Property

Public Property Get DataReceived() As Variant
'--------------------------------------------------------
' Data received by the Winsock
'--------------------------------------------------------

    DataReceived = sDataReceived

End Property

Public Sub Connect()
'--------------------------------------------------------
' Connect method of wSocket
' (overload of Connect method of sck)
'--------------------------------------------------------

    sck.Connect sck.RemoteHost, sck.RemotePort

End Sub

Public Sub SendData()
'--------------------------------------------------------
' SendData method of wSocket
' (overload of SendData method of sck)
'--------------------------------------------------------
    '
    'Send Data
    sck.SendData sDataToSend & Chr$(13) & Chr$(10)

End Sub



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Winsock TCP Listener in Excel/VBA ??

2. WinSOCK in Excel VBA

3. Winsock Control and Excel VBA

4. winsock using VBA

5. I have problems with winsock.dll in VB and VBA

6. Excel 97 Winsock

7. WinSock Call from Excel

8. Excel + Winsock ?

9. WinSock and Excel

10. Problem with winsock control, if used with excel through VB automation


 
Powered by phpBB® Forum Software © phpBB Group