CodeTransfer

Transfers VBA code components (classes, modules, forms, workbook code) from a source workbook to a target workbook using temporary file export/import cycles. Each component is exported to a temp file via the ITemporaryRepos, imported into the target, and the temp file is cleaned up. Consumers create an instance via Create and then call the transfer methods for each component type.

Depends on: ITemporaryRepos

Version: 1.0 (2026-02-09)

Instantiation

create #

Create a CodeTransfer instance

Signature:

Public Function Create(ByVal sourceWkb As Workbook, _
                       ByVal targetWkb As Workbook, _
                       ByVal tempRepos As ITemporaryRepos) As ICodeTransfer

Factory method and Friend bindings for construction. Creates a new CodeTransfer wired to transfer components from sourceWkb to targetWkb using tempRepos for intermediate file storage. All three parameters are validated as non-Nothing.

Parameters:

  • sourceWkb: Workbook. The workbook containing the VBA components to export.
  • targetWkb: Workbook. The workbook that will receive the imported components.
  • tempRepos: ITemporaryRepos. The temporary file repository for export/import.

Returns: ICodeTransfer. A fully initialised instance ready for transfers.

Throws:

  • ProjectError.ObjectNotInitialized When any parameter is Nothing.

Operations

transfer-class #

Transfer a class module from source to target workbook

Signature:

Public Sub TransferClass(ByVal className As String)

Public methods for transferring VBA components. Exports the named class module (.cls) from the source workbook to a temporary file named "CopieCls.cls", then imports it into the target workbook. The temporary file is cleaned up after import.

Parameters:

  • className: String. The VBA component name of the class to transfer.

transfer-module #

Transfer a standard module from source to target workbook

Signature:

Public Sub TransferModule(ByVal moduleName As String)

Exports the named standard module (.bas) from the source workbook to a temporary file named "CopieMod.bas", then imports it into the target workbook.

Parameters:

  • moduleName: String. The VBA component name of the module to transfer.

transfer-form #

Transfer a UserForm from source to target workbook

Signature:

Public Sub TransferForm(ByVal formName As String)

Exports the named UserForm (.frm/.frx) from the source workbook to temporary files, imports it into the target workbook, then deletes both the .frm and .frx temporary files.

Parameters:

  • formName: String. The VBA component name of the form to transfer.

transfer-workbook-code #

Copy code from a source module into the target's ThisWorkbook component

Signature:

Public Sub TransferWorkbookCode(ByVal moduleName As String)

Reads all code lines from the named module in the source workbook and writes them into the target workbook's ThisWorkbook code module, replacing any existing content. Does not create a new component; instead overwrites the built-in ThisWorkbook code module.

Parameters:

  • moduleName: String. The source module whose code will be copied.

copy-module-text #

Copy code text from a source module to an existing target module

Signature:

Public Sub CopyModuleText(ByVal moduleName As String)

Reads all code from the named module in the source workbook and writes it into the same-named module in the target workbook, replacing existing content. Used for template workbooks where the target module already exists.

Parameters:

  • moduleName: String. The module name (must exist in both workbooks).

copy-module-code #

Copy code between components within the target workbook

Signature:

Public Sub CopyModuleCode(ByVal sourceName As String, ByVal targetName As String)

Within the target workbook, reads code from the source component, writes it into the target component (replacing any existing content), then removes the source component from the VBProject. Useful after importing a module that should be injected into a different component (e.g., a worksheet module).

Parameters:

  • sourceName: String. The component to read code from and then remove.
  • targetName: String. The component to write code into.

Throws:

  • ProjectError.ObjectNotInitialized When source or target component is not found in the target workbook.

Internal members (not exported)

Bindings

bin-source-wkb #

Assign the source workbook

Signature:

Friend Property Set BinSourceWkb(ByVal value As Workbook)

Friend setters used only during factory construction.

Parameters:


bin-target-wkb #

Assign the target workbook

Signature:

Friend Property Set BinTargetWkb(ByVal value As Workbook)

Parameters:


bin-temp-repos #

Assign the temporary repository

Signature:

Friend Property Set BinTempRepos(ByVal value As ITemporaryRepos)

Parameters:


Helpers

export-import-component #

Export a VBA component from source and import into target

Signature:

Private Sub ExportImportComponent(ByVal componentName As String, ByVal tempFileName As String)

Private utilities for export/import and error handling. Exports the named component to a temporary file, imports it into the target workbook, then cleans up the temporary file. Used by TransferClass and TransferModule.

Parameters:


throw-error #

Raise a ProjectError-based exception

Signature:

Private Sub ThrowError(ByVal errNumber As Long, ByVal errorMessage As String)

Parameters:


InterfaceImplementation

ICodeTransfer_TransferClass #

Signature:

Private Sub ICodeTransfer_TransferClass(ByVal className As String)

Delegated members satisfying the ICodeTransfer contract. See the corresponding Public members above for full documentation.


Used in (4 file(s))