Settings: I have many projects with many user formats, and when I open them on a dual monitor system, by default StartUpPosition is 1 = CenterOwner, and it appears to the right of the main screen (in the center of the Windows desktop). I am trying to write a small piece of code that modifies one property for each user form in a project. In this case, in particular .StartUpPosition = 2 (CenterScreen)
Using Microsoft Office Professional Plus 2010
I already know how to change StartUpPosition using the property editor in VBA, at Positions.StartUpPosition. The problem is that there are so many custom forms for many projects, I want to change them all in each project at once. Ultimately, I want to use the same code to change any property using method overload arguments (FormName, Property, Value). At the moment, I'm fine with only one to handle StartUpPosition.
When I run this code, when I open the UserForm, it works as expected, overriding the default value for StartUpPosition = 1 (CenterOwner), and it displays the form in the center of the screen.
Sub UserForm_Initialize() With UserFormName .StartUpPosition = 2 'CenterScreen' End With End Sub
However, when I run it, built into this loop, which cycles through the project controls from the module, in an attempt to change the default values โโof all forms at once, I get the following error.
Error: Runtime error '438': The object does not support this property or method.
Sub UserFormStartUp_Center() Dim VBComp As Object 'For each control in project' For Each VBComp In Application.VBE.ActiveVBProject.VBComponents 'Check to see if control is a UserForm' If VBComp.Type = 3 Then '3 = vbext_ct_MSForm' 'Change Property - StartUpPosition - SAME AS ABOVE' With VBComp .StartUpPosition = 2 'CenterScreen' End With End If 'Loop through controls' Next End Sub
Question:. How can I set a property for a form that will be stored as the default value, and not just in the instance at runtime, in addition to entering each user character and changing it manually, scrolling to the property and clicking on it, and then closing shape and go to the next repeat. (Yes, I would do this by now, but I have many projects that will require changes to UserForms, as I am learning new methods, I need to plan other projects for clients)
I have a feeling that I do not understand the runtime. I should assume that you can set this property programmatically, since it saves it with other properties.
Study:
MSDN: property of a StartUp object According to MSDN, only objects at run time can be StartUp objects.
MSDN: property StartUpPosition
Thanks in advance for any help on this. This will save me many hours of depression.
EDIT: Adding the following after reading the answers:
UPDATE:. When I run the code with each of the sentences mentioned in the answer, I still get an error message. Runtime error: "-2147467259 (80004005)" The properties of the method "object" _VBComponent failed.
So, I decided to try a few things, for example, print the MsgBox from the Item Item. value, name, etc.
For Each VBComp In ActiveWorkbook.VBProject.VBComponents '~~> Check to see if control is a UserForm' If VBComp.Type = 3 Then With VBComp MsgBox (VBComp.Properties.Item(50).Value) End With End If Next
When I do this, it is interesting. A message box appears with the correct information that corresponds to the Locals window for this item. THEN, AFTER the msgbox message, gives an object error. If this is an error, why is the message box printed correctly? It is as if UserForm is an object, but Property.Item is not. However, it has parameters that can be defined, such as Name, Value, etc.
Screenshot included in locale information about the Item property, where Object Type = Nothing
