Private Sub REGISTER(REGFILE As String, FORCEREGISTER As Integer)

'eg. REGISTER "MSBIND.dll", 0

'The function of this subroutine is to fully register a .dll or .ocx type file
'within the registry of a Win98/XP/7 machine.
'The function is called in code by, for example,
'REGISTER "MSBIND.dll" , 0
'The "0" integer value (which can also be "1") sets the value of integer variable
'FORCEREGISTER. This is used later in the subroutine. Its "standard" value is "0"
'If FORCEREGISTER is set to 1  - (REGISTER "MSBIND.dll" , 1) - then registration /
're-registration is forced even if REGFILE is already contained within one of the
'system folders.
'Within this subroutine that file is placed into String variable REGFILE
'It is assumed that a copy of MSBIND.dll had been placed within the program folder
'from which the .exe was to be run.
'In this example, that folder is named C:\RP Program.
'(If different from this, modify the code at Line5).
'The routine checks for existence on current machine of
'C:\Windows\System, System32 and SysWOW64 folders. Depending on the op.sys. these
'contain the 32 bit .dll / .ocx files.
'For each folder which exists, the routine checks whether, if it exists, it contains
'REGFILE. If it does not, then REGFILE is copied into that folder.
'Finally, if the routine found any of these three folders which did not previously
'contain REGFILE, then using the SHELL function, Regsvr32 is run in silent mode to
'register REGFILE. It has been found that after initial full registration of a .dll
'it is necessary to close and re-start a program. A message box is shown
'to warn the user to this effect.
'If the routine finds that REGFILE is present in each of the folders which exist on
'the current machine, then Regsvr32 is NOT run, unless FORCEREGISTER was set to "1"
'in the calling code.


    Dim TESTPATH As String
    
    Dim FILEPATH As String
    
    Dim FILEPATH1 As String
    
    Dim REGISTERFILE As Integer
    
    REGISTERFILE = 0

    
    'Check whether REGFILE is listed within the C:\RP Program\ folder
    
Line5:

    FILEPATH = "C:\RP Program\" & REGFILE
    
    TESTPATH = Dir(FILEPATH)
    
    If TESTPATH = "" Then   'ie. REGFILE was not located in C:\RP Program folder
    
        If FORCEREGISTER = 1 Then

F8Register001:

            MsgBox "RP ERROR. File " & REGFILE & " has not been located within Application Program Folder." & _
            vbCrLf & vbCrLf & "(F8Register 001)"
            
        End If
        
        GoTo Line1000       'so exit routine. No point in continuing
        
    End If
    
    'First check if C:\Windows\System folder exists on this machine
    'Main win98 folder for .dll .ocx etc.)
    
    TESTPATH = Dir("C:\Windows\System", vbDirectory)
    
    If TESTPATH <> "" Then      'ie. C:\Windows\System does exist on this machine
    
        FILEPATH1 = "C:\Windows\System\" & REGFILE
        
        TESTPATH = Dir(FILEPATH1, vbNormal Or vbReadOnly Or vbHidden Or vbSystem)
        
        If TESTPATH <> "" Then GoTo Line10    'REGFILE is already copied to System
                                              'folder, so move on
                                                
        'REGFILE is not yet in the System folder, so copy it to there.
                                                
        FileCopy FILEPATH, "C:\Windows\System\" & REGFILE
        
        REGISTERFILE = 1    'flag up possible need to register REGFILE
        
    End If
        
Line10:

    'next check if C:\Windows\System32 folder exists
    'main XP folder for 32 bit .dll .ocx etc.)
    
    TESTPATH = Dir("C:\Windows\System32", vbDirectory)
    
    If TESTPATH <> "" Then      'ie. C:\Windows\System32 does exist
    
        FILEPATH1 = "C:\Windows\System32\" & REGFILE
        
        TESTPATH = Dir(FILEPATH1, vbNormal Or vbReadOnly Or vbHidden Or vbSystem)
        
        If TESTPATH <> "" Then GoTo Line20      'REGFILE already copied to System32
                                                'folder, so move on
                                                
        'REGFILE is not yet in the System32 folder, so copy it to there.
                                                
        FileCopy FILEPATH, "C:\Windows\System32\" & REGFILE
        
        REGISTERFILE = 1    'flag up possible need to register REGFILE
        
    End If
    
Line20:
 
    'next check if C:\Windows\SysWOW64 folder exists.
    'This is used in Win7 (& Vista?) to contain 32 bit .dll / .ocx etc.
    
    TESTPATH = Dir("C:\Windows\SysWOW64", vbDirectory)
    
    If TESTPATH <> "" Then      'ie. C:\Windows\SysWOW64 does exist
    
        FILEPATH1 = "C:\Windows\SysWOW64\" & REGFILE
        
        TESTPATH = Dir(FILEPATH1, vbNormal Or vbReadOnly Or vbHidden Or vbSystem)
        
        If TESTPATH <> "" Then GoTo Line30      'REGFILE already copied to SysWOW64
                                                'folder, so move on
                                                
        'REGFILE is not yet in the SysWOW64 folder, so copy it to there.
                                                
        FileCopy FILEPATH, "C:\Windows\SysWOW64\" & REGFILE
        
        REGISTERFILE = 1    'flag up possible need to register REGFILE
        
    End If
    
Line30:

    If FORCEREGISTER = 1 Then GoTo Line40   'force registration/re-registration

    If REGISTERFILE = 0 Then GoTo Line1000  'no requirement to register REGFILE

Line40:

    Dim SHELLROUTINE As String
    
    SHELLROUTINE = "Regsvr32 /s " & REGFILE 'the /s causes Regsvr32 to run silent

    Shell SHELLROUTINE
    
F8Register002:
    
    MsgBox "Registration of " & REGFILE & " completed. Please now CLOSE and RE-START the program" & _
    vbCrLf & vbCrLf & "(F8Register002)"
    
     
Line1000:

End Sub