Importing and Exporting UserForms and Modules

3.1k views Asked by At

HELP! I cannot find this anywhere... I have found plenty of explanations of .frx files but that is not the issue. I have exported 2 modules ".bas" and a userform ".frm" that also created the ".frx" binary file. The issue is that now when I import them to a new workbook with the code below, upon initializing the userform, the application opens and tries to run in the original workbook, in which I wrote the modules and designed the custom userform. Why is this happening and is there a way to get it to load the userform in the workbook that the form and modules were imported into?

Sub Import_VBA(WBName)

    Dim VBc As Variant
    Dim exportFolder As String, VBcExt As String, testFile As String
    Dim newWB As Workbook

    testFile = WBName
    exportFolder = "y:\ECI\Database\Outputs\InterfaceApp"
    Set newWB = Workbooks(testFile & ".xlsm")

    '''''  Test VBA protection
    On Error Resume Next
    If newWB.VBProject.Protection <> 0 Then

        If Err.Number = 1004 Then
            Err.Clear
            MsgBox "VBA Project Object Model is protected in " & newWB.Name & vbCrLf _
            & vbCrLf & "Please remove VBA protection in Trust Center to continue.", _
            vbExclamation + vbOKOnly, "Error"

            Set newWB = Nothing
            Exit Sub
        Else
            MsgBox Err.Number & ": " & Err.Description, vbExclamation, "Error"
            Set newWB = Nothing
            Err.Clear
            Exit Sub
        End If

    End If

    ''''' Add Interface App Components
    For Each VBc In CreateObject("Scripting.FileSystemObject").GetFolder(exportFolder).Files
        Select Case LCase(Right(VBc.Name, 4))
        Case ".bas", ".frm", ".cls", ".frx"
            newWB.VBProject.VBComponents.Import exportFolder & "\" & VBc.Name
        End Select
    Next VBc
End Sub
0

There are 0 answers