Reputation: 59
Compile error:
Cannot define a Public user-defined type within an object module
What does the above compile error mean in Visual Basic for Applications (VBA)?
I would like to point out that the exact same code had no error in a separate empty Powerpoint file.
Thank you.
Upvotes: 1
Views: 3126
Reputation: 29466
You need to understand the different types of code modules in VBA. Basically, you can have
Depending on the environment, you can have build-in modules. In Excel, these are Workbook and Worksheet modules, in Word the Document module (as far as I know in Powerpoint, there are no build in modules).
Now, all of the above, except for a regular module, define classes (user forms, worksheets, workbooks, documents are special kind of classes). And a class cannot contain a public type (it is already a public type), as simple as that. It's similar to the fact that you cannot define a Type within a Type.
I assume that you have your type defined in a user form code. If you need the type only within that form, declare it as private. It is allowed to define types within a class, but they need to be private, with other words, the type in unknown in other modules of your project, and, as a consequence, you cannot have public variables of that type or public functions that return that type.
If you need the type in more than one module, you need to move the type definition into a regular module (this is what GSerg wrote in the comments)
Upvotes: 6