Excel

Install or Uninstall ActiveX Controls (.ocx, .dll files)

Ease of Use

Intermediate

Version tested with

2000 

Submitted by:

johnske

Description:

Allows you to install and register ActiveX controls automatically. 

Discussion:

You may want to install and register ActiveX controls that are required for your project without having to explain a manual procedure for the end-user (n.b. novices may find this procedure daunting and could very well be messed up anyway). The functions and procedures given here can be readily adapted to take care of all this automatically. In the example given in the attachment, the popular Microsoft MonthView Control contained in the mscomct2.ocx file is installed and registered on your machine. This particular ocx file also contains several other controls, (details listed in the attachment. An example of the MonthView control is found here > http://www.vbaexpress.com/kb/getarticle.php?kb_id=431). There are buttons included to test out the options Install/Register and Uninstall/DeRegister, or simply Register/DeRegister. 

Code:

instructions for use

			

'NOTE: YOU CAN CHANGE THE NAME OF THE .OCX OR .DLL FILE 'IN THE ZIPPED EXAMPLE TO ONE OF YOUR OWN CHOICE. '***************CODE FOR MODULE1*************** Option Explicit Sub PutFileInSystem() Dim FileSysObject As Object Dim FileName$, FilesOldPath$, FilesNewPath$ FileName = [D3] FilesOldPath = ActiveWorkbook.Path & "\" FilesNewPath = "C:\Windows\System\" Set FileSysObject = CreateObject("Scripting.FileSystemObject") If Not FileSysObject.FileExists(FilesOldPath & FileName) Then MsgBox "The file " & FilesOldPath & FileName & " was not found...", , _ "File Is Missing" ElseIf Not FileSysObject.FileExists(FilesNewPath & FileName) Then 'move the file to the new location FileSysObject.MoveFile (FilesOldPath & FileName), FilesNewPath & FileName MsgBox FilesOldPath & FileName & vbLf & vbNewLine & _ "has been installed in the location given below:" & vbLf & vbNewLine & _ FilesNewPath & FileName Else MsgBox "Sorry, the install cannot be performed. There is" & vbLf & _ "already a " & FilesNewPath & FileName, , "Destination File Already Exists" End If RegisterIt End Sub Sub TakeFileFromSystem() Dim FileSysObject As Object Dim FileName$, FilesOldPath$, FilesNewPath$ FileName = [D3] FilesOldPath = "C:\Windows\System\" FilesNewPath = ActiveWorkbook.Path & "\" Set FileSysObject = CreateObject("Scripting.FileSystemObject") If Not FileSysObject.FileExists(FilesOldPath & FileName) Then MsgBox "The file " & FilesOldPath & FileName & " was not found...", , _ "File Is Missing" ElseIf Not FileSysObject.FileExists(FilesNewPath & FileName) Then 'move the file to the new location On Error GoTo ErrorMsg FileSysObject.MoveFile (FilesOldPath & FileName), FilesNewPath & FileName MsgBox FilesOldPath & FileName & vbLf & vbNewLine & _ "has been moved to the location given below:" & vbLf & vbNewLine & _ FilesNewPath & FileName Else MsgBox "Sorry, the file removal cannot be performed. There is an existing " & _ FileName & vbLf & _ "file in " & FilesNewPath & " please remove it first", , "File In The Way..." End If DeRegisterIt Exit Sub ErrorMsg: MsgBox "This workbook has a reference set to the file you're trying to uninstall, " _ & vbLf & "you will need to go into the VBE window, select Tools/References and " _ & vbLf & "uncheck that particular reference before you can uninstall the file." End End Sub Sub RegisterIt() Dim Tmp$, FilesName$, Ref As Object Dim FileSysObject As Object Const FilesPath = "C:\Windows\System\" FilesName = [D3] Set FileSysObject = CreateObject("Scripting.FileSystemObject") If Not FileSysObject.FileExists(FilesPath & FilesName) Then MsgBox "The file " & FilesPath & FilesName & " was not found...", , _ "Cannot Be Registered" Exit Sub End If Tmp = Register("c:\windows\system\" & FilesName) MsgBox FilesName & " Registered" End Sub 'Note: Different to registering in this respect. 'The file may've already been removed (say, 'manually) and we just want to de-register it Sub DeRegisterIt() Dim Tmp$, FilesName$, Ref As Object FilesName = [D3] Tmp = DeRegister("c:\windows\system\" & FilesName) MsgBox FilesName & " Deregistered" End Sub '********************************************** '**********CODE FOR MODULE2 (Functions)********** Option Explicit 'All required Win32 SDK functions to register/unregister any ActiveX component Private Declare Function LoadLibraryRegister Lib "KERNEL32" Alias _ "LoadLibraryA" (ByVal lpLibFileName$) As Long Private Declare Function FreeLibraryRegister Lib "KERNEL32" Alias _ "FreeLibrary" (ByVal hLibModule&) As Long Private Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject&) As Long Private Declare Function GetProcAddressRegister Lib "KERNEL32" Alias _ "GetProcAddress" (ByVal hModule&, _ ByVal lpProcName$) As Long Private Declare Function CreateThreadForRegister Lib "KERNEL32" Alias _ "CreateThread" (lpThreadAttributes As Any, _ ByVal dwStackSize&, ByVal lpStartAddress&, _ ByVal lpparameter&, ByVal dwCreationFlags&, _ ThreadID&) As Long Private Declare Function WaitForSingleObject Lib "KERNEL32" (ByVal hHandle&, _ ByVal dwMilliseconds&) As Long Private Declare Function GetExitCodeThread Lib "KERNEL32" (ByVal Thread&, _ lpExitCode&) As Long Private Declare Sub ExitThread Lib "KERNEL32" (ByVal ExitCode&) Private Const STATUS_WAIT_0 = &H0 Private Const WAIT_OBJECT_0 = ((STATUS_WAIT_0) + 0) Public Const DllRegisterServer = 1 Public Const DllUnRegisterServer = 2 Function Register(FileName$) As String If Dir(FileName) = Empty Then Register = "File not found" Exit Function Else Register = RegisterFile(FileName, DllRegisterServer) End If End Function Function DeRegister(FileName$) As String If Dir(FileName) = Empty Then DeRegister = "File not found" Exit Function Else DeRegister = RegisterFile(FileName, DllUnRegisterServer) End If End Function Function RegisterFile(ByVal FileName$, ByVal RegFunction&) As String Dim LoadLib&, ProcAddress&, ThreadID&, Successful&, ExitCode&, Thread& If FileName = Empty Then Exit Function LoadLib = LoadLibraryRegister(FileName) If LoadLib = 0 Then RegisterFile = "File Can't Be Loaded" 'Couldn't load component Exit Function End If If RegFunction = DllRegisterServer Then ProcAddress = GetProcAddressRegister(LoadLib, "DllRegisterServer") ElseIf RegFunction = DllUnRegisterServer Then ProcAddress = GetProcAddressRegister(LoadLib, "DllUnregisterServer") End If If ProcAddress = 0 Then RegisterFile = "Not ActiveX Component" If LoadLib Then FreeLibraryRegister (LoadLib) Exit Function Else Thread = CreateThreadForRegister(ByVal 0&, 0&, ByVal ProcAddress, _ ByVal 0&, 0&, ThreadID) If Thread Then Successful = (WaitForSingleObject(Thread, 10000) = WAIT_OBJECT_0) If Not Successful Then Call GetExitCodeThread(Thread, ExitCode) ExitThread (ExitCode) RegisterFile = "Component Registration Failed" If LoadLib Then FreeLibraryRegister (LoadLib) Exit Function Else If RegFunction = DllRegisterServer Then RegisterFile = Empty 'registered successfully ElseIf RegFunction = DllUnRegisterServer Then RegisterFile = Empty 'unregistered successfully End If End If CloseHandle (Thread) If LoadLib Then FreeLibraryRegister (LoadLib) End If End If End Function '**********************************************

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Insert/Module
  4. Copy and paste the code for module1 into this Module
  5. Select Insert/Module once again (you need two modules0
  6. Copy and paste the code for module2 into this new Module
  7. Now select File/Close and Return To Microsoft Excel
  8. Don't forget to save your changes...
 

Test the code:

  1. Download the attachment, open the enclosed workbook
  2. Click 'Install & Register The File Above
  3. The ocx file will now be installed & registered...(n.b. it disappears from the folder)
  4. You can check this by going to View/Toolbars/Control Toolbox, now click the bottom symbol 'More Controls' (crossed spanner & hammer symbol) and scroll down. If you see Microsoft MonthView Control 6.0 (SP4) in the list, it has been registered successfully and is now available to use on userforms and/or spreadsheets.
  5. Now click 'Uninstall & Deregister The File Above' and repeat the procedure above. You'll see that MonthView Control is no longer on the list.
  6. NOTE: When installed, these new controls will be found in the VBE window listed under 'Additional Controls'
  7. (You can also Install/Uninstall, or just Register/Deregister any other ocx or dll files by placing them in the 'Install ActiveX' Folder and entering their name in D3)
 

Sample File:

Install ActiveX.zip 349.67KB 

Approved by mdmackillop


This entry has been viewed 287 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express