VBA TIPS
What is a Class? – Basic understanding of a Class Module – Chapter1

Introduction

The term Class Module may give you an impression that it must be difficult. There might still be many Excel users who have never used the Class Module as yet because general VBA programs for Excel can be written only using Standard Modules, UserForm Modules or WorkSheet Modules.

However, once you?fve got what the Class Module is, it helps to understand what the Object Oriented Programming is. Besides, a Class Module can be used for any kind of programs. It's not very difficult one so let's give it a try.

What can be done with Class Modules? The following code would get the IP address of your machine.

Note: you can also use the GetPcName function alone when you'd like to know the name of the machine.


Example

Place the following in a standard module.

Option Explicit

Private Const IP_SUCCESS As Long = 0
Private Const WS_VERSION_REQD As Long = &H101
Private Const MIN_SOCKETS_REQD As Long = 1
Private Const SOCKET_ERROR As Long = -1
Private Const INADDR_NONE As Long = &HFFFFFFFF
Private Const MAX_WSADescription As Long = 256
Private Const MAX_WSASYSStatus As Long = 128
Private Type WSADATA
    wVersion As Integer
    wHighVersion As Integer
    szDescription(0 To MAX_WSADescription) As Byte
    szSystemStatus(0 To MAX_WSASYSStatus) As Byte
    wMaxSockets As Long
    wMaxUDPDG As Long
    dwVendorInfo As Long
End Type
Private Declare Function gethostbyname Lib "WSOCK32.DLL" _
                                       (ByVal hostname As StringAs Long
Private Declare Sub CopyMemory Lib "kernel32" _
                               Alias "RtlMoveMemory" _
                               (xDest As Any, _
                                xSource As Any, _
                                ByVal nbytes As Long)
Private Declare Function WSAStartup Lib "WSOCK32.DLL" _
                                    (ByVal wVersionRequired As Long, _
                                     lpWSADATA As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function inet_addr Lib "WSOCK32.DLL" _
                                   (ByVal s As StringAs Long
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
                                         (ByVal Buffer As String, _
                                          Size As LongAs Long


Sub TestingFunction()
    If SocketsInitialize() Then
        MsgBox "IP address of " & GetPcName & " is " & GetIPFromHostName(GetPcName)
    End If
    SocketsCleanup
End Sub

Public Function GetIPFromHostName(ByVal sHostName As StringAs String
'converts a host name to an IP address.
    Dim nbytes As Long
    Dim ptrHosent As Long  'address of hostent structure
    Dim ptrName As Long    'address of name pointer
    Dim ptrAddress As Long    'address of address pointer
    Dim ptrIPAddress As Long
    Dim sAddress As String
    sAddress = Space$(4)
    ptrHosent = gethostbyname(sHostName & vbNullChar)
    If ptrHosent <> 0 Then
        ptrName = ptrHosent
        ptrAddress = ptrHosent + 12
        'get the IP address
        CopyMemory ptrName, ByVal ptrName, 4
        CopyMemory ptrAddress, ByVal ptrAddress, 4
        CopyMemory ptrIPAddress, ByVal ptrAddress, 4
        CopyMemory ByVal sAddress, ByVal ptrIPAddress, 4
        GetIPFromHostName = IPToText(sAddress)
    End If
End Function

Private Function IPToText(ByVal IPAddress As StringAs String
    IPToText = CStr(Asc(IPAddress)) & "." & _
               CStr(Asc(Mid$(IPAddress, 2, 1))) & "." & _
               CStr(Asc(Mid$(IPAddress, 3, 1))) & "." & _
               CStr(Asc(Mid$(IPAddress, 4, 1)))
End Function

Public Sub SocketsCleanup()
    If WSACleanup() <> 0 Then
        MsgBox "Windows Sockets error occurred in Cleanup.", vbExclamation
    End If
End Sub

Public Function SocketsInitialize() As Boolean
    Dim WSAD As WSADATA
    SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
End Function

Public Function GetPcName() As String
    Dim strBuf As String * 16, strPcName As String, lngPc As Long
    lngPc = GetComputerName(strBuf, Len(strBuf))
    If lngPc <> 0 Then
        strPcName = Left(strBuf, InStr(strBuf, vbNullChar) - 1)
        GetPcName = strPcName
    Else
        GetPcName = vbNullString
    End If
End Function

| HOME |
Copyright © cellmasters.net - colo's junk room All Right Reserved
ABOUT
WORKS
THE CELL MASTERS
CONTACT
LINKS
Tips and Information about Microsoft Excel|Masaru Kaji aka Colo