Change StartUpPosition Evey UserForm in Project ERROR: object does not support property

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

Locals info

+5
source share
1 answer

Each UserForm / Control element has a property that can be accessed by .Properties.Item

for instance

 Sub GetPropertiesDetails() Dim VBComp As Object Dim i As Long, j As Long i = 1 For Each VBComp In ActiveWorkbook.VBProject.VBComponents '~~> Check to see if control is a UserForm' If VBComp.Type = 3 Then With VBComp For j = 1 To .Properties.Count Debug.Print i & ". "; .Properties.Item(j).Name i = i + 1 Next j End With Exit For '<~~ Just want to check for one userform End If Next End Sub 

When you run the above code, you will get it in the "Direct" window

 1. ActiveControl 2. BackColor 3. BorderColor 4. BorderStyle 5. CanPaste 6. CanRedo 7. CanUndo 8. Controls 9. Cycle 10. _Font_Reserved 11. Font 12. ForeColor 13. InsideHeight 14. InsideWidth 15. KeepScrollBarsVisible 16. MouseIcon 17. MousePointer 18. PictureAlignment 19. Picture 20. PictureSizeMode 21. PictureTiling 22. ScrollBars 23. ScrollHeight 24. ScrollLeft 25. ScrollTop 26. ScrollWidth 27. Selected 28. SpecialEffect 29. VerticalScrollBarSide 30. Zoom 31. DesignMode 32. ShowToolbox 33. ShowGridDots 34. SnapToGrid 35. GridX 36. GridY 37. DrawBuffer 38. Name 39. Caption 40. Left 41. Top 42. Width 43. Height 44. Enabled 45. Tag 46. HelpContextID 47. WhatsThisButton 48. WhatsThisHelp 49. RightToLeft 50. StartUpPosition 51. ShowModal 

So, from this we see that the property that we have after is at 50 . Now all we need to do is use .StartUpPosition = 2 instead, like you.

 Sub SetUserformStartUp() Dim VBComp As Object For Each VBComp In ActiveWorkbook.VBProject.VBComponents '~~> Check to see if control is a UserForm' If VBComp.Type = 3 Then VBComp.Properties.Item(50).Value = 2 Next End Sub 

Courtesy of Chris Nielsen (From Comments)

You can also use:

 VBComp.Properties.Item("StartUpPosition") = 2 
+6
source

Source: https://habr.com/ru/post/1205999/


All Articles