Suppose I have a simple VBA project containing a UserForm called UserForm1
If I pass UserForm1 to a function as a Userform
type variable I cannot access all the members of the UserForm
, for example the the .Height
property, as you see in this simple example.
In order to get this working I need to pass the UserForm as an Object
, but that's pretty annoying since I lose all the autocomplete features of the IDE.
Sub CATMain()
Call printFormHeight_Works(UserForm1)
Call printFormHeight_Broken(UserForm1) '--> error
End Sub
Sub printFormHeight_Broken(ByRef form As UserForm)
Debug.Print form.Caption '---> works!
Debug.Print form.Height '---> Run-time error 438: Object doesn't support this property or method
End Sub
Sub printFormHeight_Works(ByRef form As Object)
Debug.Print form.Caption '---> works!
Debug.Print form.Height '---> works!
End Sub
So my questions are:
- Why I have to pass the Userform as an Object?
- Is there a better way to pass a Userform to a function and be able to access all its members?