MSWinsock.Winsock event handling in VisualBasic

24,118

Are you stuck using MSWinsock?
Here is a site/tutorial using a custom winsock object.

Also... You need to declare Winsock1 WithEvents within a "Class" module:

Private WithEvents Winsock1 As Winsock

And finally, make sure you reference the winsock ocx control.
Tools -> References -> Browse -> %SYSEM%\MSWINSCK.OCX

Share:
24,118
user21530
Author by

user21530

Updated on November 13, 2020

Comments

  • user21530
    user21530 over 3 years

    I'm trying to handle Winsock_Connect event (Actually I need it in Excel macro) using the following code:

    Dim Winsock1 As Winsock 'Object type definition
    
    Sub Init()
        Set Winsock1 = CreateObject("MSWinsock.Winsock") 'Object initialization
        Winsock1.RemoteHost = "MyHost"
        Winsock1.RemotePort = "22"
        Winsock1.Connect
    
        Do While (Winsock1.State <> sckConnected)
            Sleep 200
        Loop
    End Sub
    
    'Callback handler
    Private Sub Winsock1_Connect()
        MsgBox "Winsock1::Connect"
    End Sub
    

    But it never goes to Winsock1_Connect subroutine although Winsock1.State is "Connected". I want to use standard MS library because I don't have administrative rights on my PC and I'm not able to register some custom libraries. Can anybody tell me, where I'm wrong?

  • ctbrown
    ctbrown over 9 years
    The link to the 'site/tutorial' seems to be down, likely for good.
  • addohm
    addohm about 6 years
    @Nescio I can't seem to find this file anywhere on a W7 64-bit PC. How can I add the reference?
  • Shidomaru NeveRage
    Shidomaru NeveRage about 4 years
    It's not clear at all for me, how do we do "You need to declare Winsock1 WithEvents within a "Class" module:".