Register/Unregister ActiveX Controls Using Code

This example will show you how to register and unregister ActiveX controls from Visual Basic using code, instead of using the Regsvr32.exe

Preparations

Add 2 Command Buttons to your form.
Press the first to unregister the control, and the second to register it.

Module Code

'replace the two "d:\MyDir\MyFile.OCX" below with the name of the
'OCX file you want to register or unregister

Public Declare Function RegControl Lib "d:\MyDir\MyFile.OCX" _
    Alias "DllRegisterServer" () As Long
 
Public Declare Function UnRegControl Lib "d:\MyDir\MyFile.OCX" _
   Alias "DllUnregisterServer" () As Long
 
Public Const S_OK = &H0
 
Sub RegisterControlSub()
   On Error GoTo Err_Registration_Failed
   If RegControl = S_OK Then
      MsgBox "Registered Successfully"
   Else
      MsgBox "Not Registered"
   End If
   Exit Sub
Err_Registration_Failed:
   MsgBox "Error: " & Err.Number & " " & Err.Description
End Sub
 
Sub UnRegisterControlSub()
   On Error GoTo Err_Unregistration_Failed
   If UnRegControl = S_OK Then
      MsgBox "Unregistered Successfully"
   Else
      MsgBox "Not Unregistered"
   End If
   Exit Sub
Err_Unregistration_Failed:
   MsgBox "Error: " & Err.Number & " " & Err.Description
End Sub

Form Code

Private Sub Command1_Click()
    UnRegisterControlSub
End Sub

Private Sub Command2_Click()
    RegisterControlSub
End Sub

Go Back