Components

Importing and Exporting components is useful if you want to reuse a component from another project.
Every object in a project can be saved to a separate file.
Saving an individual object in a project is known as exporting.
This is useful if you want to use a particular object in a different project.
If you want to copy a module from one project to another you do not have to export and import each component.
If you open both projects you can use the Projects Explorer window to drag the components from one project to the other.


File Extensions

.frmA Userform file
.frxA Binary data file
.clsA Class module file
.basA Standard module file

Importing Components

To import a file into a project highlight the project in the Project Explorer window and select (File > Import File).
Alternatively you can use the shortcut key (Ctrl + M).
You can only import a file that has been exported with the (File > Export) command.
You can then import these ".bas" files into other projects.


Userforms

Public Function ImportFiles( _ 
         ByRef objComponents As VBIDE.VBComponents, _
         ByVal sImportPath As String, _
         ByVal bDeleteCodeFiles As Boolean)

Dim lCount As Long
Dim sFullName As String
Dim sComponentName As String

   For lCount = all the items

       sComponentName =
       sFullName = sImportPath & sComponentName

       objComponents.Import sFullName
   Next
End Function

Exporting Components

Every object in a Project can be saved to a separate file.
Saving an object to an separate file is known as exporting.
To export an object (File > Export) or (Ctrl + E).
You will get a dialog box asking for the filename.
A copy of the file is then moved. The original file is not moved.


Userforms

When you export a Userform any code associated with that userform will also get exported.
.frm - this is a text file that contains the userform's VBA code and textual layout,
.frx - this is a binary companion file that stores all the non-code parts of a VBA UserForm.

'add a reference to "Microsoft Visual Basic for Applications Extensibility 5.3

Public Sub ExportTheProject_VersionControl()
Dim sFolderPrefix As String
Dim sFolderPath As String

   sFolderPrefix = "C:\Temp\"
   sFolderPath = "VBA Export\"
   
   Call ExportAllModules(sFolderPrefix & sFolderPath, "_2", True)
End Sub

Public Sub ExportTheProject_OneFolder()
Dim sFolderPrefix As String
Dim sFolderPath As String

   sFolderPrefix = "C:\Temp\"
   sFolderPath = "VBA Export\"
   
   Call ExportAllModules(sFolderPrefix & sFolderPath, "", False)
End Sub

Public Sub ExportAllModules( _
         ByVal sFolderPath As String, _
         ByVal sFileSuffix As String, _
         ByVal bSaveInSubFolders As Boolean)
   
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
        
   Set VBProj = ThisWorkbook.VBProject
   Set VBProj = ActiveDocument.VBProject
        
   For Each VBComp In VBProj.VBComponents
      If ((VBComp.Type = vbext_ct_Document) Or _
          (VBComp.Type = vbext_ct_StdModule) Or _
          (VBComp.Type = vbext_ct_MSForm)) Then
          
         Set CodeMod = VBComp.CodeModule
         
         Call ExportVBComponent(VBComp, sFolderPath, sFileSuffix, VBComp.CodeModule, True, bSaveInSubFolders)
      End If
   Next VBComp
End Sub

Public Function ExportVBComponent( _
         ByVal VBComp As VBIDE.VBComponent, _
         ByVal sFolderPath As String, _
         ByVal sFileSuffix As String, _
Optional ByVal sFileName As String, _
Optional ByVal bOverwriteExisting As Boolean = True, _
Optional ByVal bSaveInSubFolders As Boolean = False) _
         As Boolean

Dim sExtension As String
Dim sFName As String

    sExtension = GetFileExtension(VBComp:=VBComp)

    sFName = sFileName
    If InStr(1, sFName, ".", vbBinaryCompare) = 0 Then
       If (sFileSuffix = "") Then
          sFName = sFName & sExtension
       Else
          sFName = sFName & sFileSuffix & sExtension
       End If
    End If

    If StrComp(Right(sFolderPath, 1), "\", vbBinaryCompare) <> 0 Then
       sFolderPath = sFolderPath & "\"
    End If
         
    If (bSaveInSubFolders = False) Then
        sFName = sFolderPath & sFName
    End If
    If (bSaveInSubFolders = True) Then
        sFName = sFolderPath & VBComp.Name & "\" & sFName
        If (Folder_Exists(sFolderPath & VBComp.Name) = False) Then
           Call MkDir(sFolderPath & VBComp.Name)
        End If
    End If
    
    If Dir(sFName, vbNormal + vbHidden + vbSystem) <> vbNullString Then
        If (bOverwriteExisting = True) Then
            Kill sFName
        Else
            ExportVBComponent = False
            Exit Function
        End If
    End If
    
    VBComp.Export FileName:=sFName
    ExportVBComponent = True
End Function
    
Public Function GetFileExtension( _
         ByVal VBComp As VBIDE.VBComponent) As String

    Select Case VBComp.Type
        Case vbext_ct_ClassModule
            GetFileExtension = ".cls"
        Case vbext_ct_Document
            GetFileExtension = ".cls"
        Case vbext_ct_MSForm
            GetFileExtension = ".txt"
        Case vbext_ct_StdModule
            GetFileExtension = ".bas"
        Case Else
            GetFileExtension = ".bas"
    End Select
End Function

Public Function Folder_Exists( _
         ByVal sFolderPath As String) As Boolean

Dim iTemp As Integer
   On Error GoTo AnError
   iTemp = GetAttr(sFolderPath)
   Folder_Exists = True
   Exit Function
AnError:
   Folder_Exists = False
End Function

Attributes

Attributes cannot be seen in the Visual Basic Editor.
Some attributes can be seen when you export a module to a text file.
Attributes can be used to set the name of a module or class, add descriptions to code resources, give classes a default global instance, set the default member of a class, and set a class's enumerator for use with For Each loops.

AttributeDescription
VB_NameUsed to set the name of the module or class.
VB_DescriptionUsed to set description text for modules, classes, procedures, and variables.
VB_PredeclaredIdSetting to True creates a default global instance of the class when the application starts.
VB_ExposedSetting to True makes a class type available outside of the VBA project. Objects of the class type can exist outside the VBA project but cannot be instantiated outside the VBA project. Instances created from an exposed class must be passed from a factory function defined within the VBA project.
VB_UserMemIdSetting to 0 makes the member the default member. Setting to -4 makes the member the enumerator of a class to be used with For Each loops.

VB_Name

This attribute can be used to set the name of a module or class.
This attribute can also be set in the Properties window in the Visual Basic Editor.


VB_Description

This attribute can be set description text for modules, classes, procedures, and variables.
When declaring the attribute for a module use the form "VB_Description".
When declaring the attribute for a procedure use the form "ProcedureName.VB_Description" inside the procedure.
When declaring the attribute for a variable use the form "VariableName.VB_VarDescription".


VB_PredeclaredId

This attribute can be used to create a default global instance of a class.
Setting this attribute to True will cause a global instance to be created when the application starts.


VB_Exposed

This attribute is used to specify the accessibility of a class.
Setting this attribute to True will make the class visible to outside VBA projects.
This has the same effect as setting the Instancing property of a class to PublicNotCreatable.
By default this property is set to False which makes the Instancing property Private.
PublicNotCreateable classes allow variables of the class type to be declared outside of the VBA project in which they are defined but objects of the class type cannot be instantiated outside the VBA project in which they are defined.


VB_UserMemId

This attribute can be used to set a default member of a class and to set an enumerator for a class to be used with For Each loops.


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext