UserForm function not listed in Macros dialog

<?xml encoding=”UTF-8″>By Adam Nagy

Let’s say we have a VBA project with a Class, a UserForm and a Module, each with a single function in it:

VBA1
When checking in the Macros dialog only the Module‘s function (MyModuleFunction) will be shown:

VBA2

This is as designed. Both the Class and the UserForm hide their properties and functions from outside because those are instance specific. In other words, if you want to call a function of a UserForm you have to create a function in a Module which will create an instance of the UserForm and through that then you can access the form’s properties and call its functions, including the one to show the form:

Public myUserForm As UserForm1
Sub MyModuleFunction()
' We create an instance of the form
Set myUserForm = New UserForm1
' Now we can access the form's functions
' and properties through the variable
' named "myUserForm"
' If you want to show it as modal,
' i.e. all the other UI is disabled
' until the dialog is dismissed, then
' just delete the word "vbModeless"
' from the below code
Call myUserForm.Show(vbModeless)
End Sub

Now if you call MyModuleFunction, it will show our form:

VBA3

  


Comments

2 responses to “UserForm function not listed in Macros dialog”

  1. Cam Hartman Avatar
    Cam Hartman

    Hey. This is a great help! However, I have now encountered a separate problem.
    When I go to execute the macro it gives the Run-time error ‘402’: Must close or hide topmost modal form first. I have the userform ask for user input to create a box in autoCAD. Obviously when I input the last section this does not allow the user to enter anything into the form. I’m not sure how to fix this.
    When I delete the from my UserForm1 script, the new error message when the user clicks “okay” on the UserForm text box I get a new run-time error ‘-2145320932 (8021001c)’: AutoCAD main window is invisible.
    Any thoughts? Thanks!

  2. Could you please log this on the Inventor Customization forum and provide more information on what exactly your code is doing and how?
    Thank you!

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading