Search code examples
vbaexceluserform

Run-time error '424' Object Required when doing userform.show on imported userform


I am attempting to import a userform, and show it on initial startup. The userform imports just fine, however, when attempting to show it, I keep getting a Run-time error '424' Object Required.

Here is my code:

Sub Workbook_Open()

Dim wkbTarget As Excel.Workbook
Dim szTargetWorkbook As String
Dim cmpComponents As VBIDE.VBComponents
Application.ScreenUpdating = False
szTargetWorkbook = ActiveWorkbook.Name
Set wkbTarget = Application.Workbooks(szTargetWorkbook)
Set cmpComponents = wkbTarget.VBProject.VBComponents

' IMPORT FORM
cmpComponents.Import "\\myserver.domain\Application\Forms\LOGIN.frm"

LOGIN.Show

End Sub

When I click End on the error, I can then show the userform just fine.


Solution

  • The run-time (thanks @Comintern!) won't like you referring to an object that doesn't exist yet. You could use:

    Userforms.add("LOGIN").Show
    

    to avoid that direct reference.