项目中更改Evey用户窗体的StartUpPosition错误:对象不支持属性

安装程序:我有许多用户窗体的项目,并在双监视器系统上打开时,默认的StartUpPosition是1 = CenterOwner,它出现在主显示器(Windows桌面的中心)的右侧。 我正在尝试编写一小段代码来更改项目中每个用户窗体的一个属性。 在这种情况下,具体来说,.StartUpPosition = 2(CenterScreen)

使用Microsoft Office Professional Plus 2010

我已经知道如何使用VBA中的属性编辑器在Positions.StartUpPosition下更改StartUpPosition。 问题是,在这么多的项目中有这么多的用户窗体,我想一次在每个项目上更改它们。 最终,我想通过使用方法(FormName,Property,Value)的重载参数来使用相同的代码来改变任何属性。 现在,我可以只用一个来处理StartUpPosition。

当我运行这个代码时,打开UserForm时,它按预期工作,覆盖StartUpPosition = 1(CenterOwner)的默认值,并将它显示在屏幕的中心。

Sub UserForm_Initialize() With UserFormName .StartUpPosition = 2 'CenterScreen' End With End Sub 

然而,当我运行它embedded在这个循环中循环通过模块的项目的控制,试图一次性改变所有表单的默认值,我得到以下错误。

错误:运行时错误“438”:对象不支持此属性或方法。

 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 

问题:如何将表单的属性设置为默认值存储,而不仅仅是在运行时的实例中,除了进入每个用户窗体并通过向下滚动到属性并单击它来手动更改之后closures表格并转到下一个重复。 (是的,现在我已经完成了,但是当我学习新技术时,我有很多项目需要更改用户窗体,我需要为客户端打补丁)

我觉得这是关于运行时间的东西,我不明白。 我必须相信,你可以通过编程方式来设置这个属性,因为它将其与其他属性一起存储。

研究:
MSDN:启动对象属性根据MSDN,只有在运行时的对象可以是启动对象。

MSDN:StartUpPosition属性

提前感谢您的帮助。 这将节省我很多小时的点击。

编辑:在阅读答案后添加以下内容:

更新:当我运行与您在答案中提到的每个build议的代码时,我仍然出现错误。 运行时错误:'-2147467259(80004005)'对象'_VBComponent'的方法'属性'失败。

所以我决定尝试一些东西,比如打印属性项的值,名称等的MsgBox。

 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 

当我这样做,这很有趣。 消息框出现,与正确的信息匹配该项目的本地窗口。然后,在msgbox后,它给一个对象的错误。 如果这是一个错误,那么为什么消息框打印正确? 就好像UserForm是一个对象,但Property.Item不是。 然而,它具有可以定义的参数,如名称,值等。

该属性包含当地人信息的屏幕截图对象types=无

当地人信息

每个UserForm / Control都有一个属性,可以通过.Properties.Item访问

例如

 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 

当你运行上面的代码,你会在立即窗口中得到这个

 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 

因此,我们看到,我们以后的财产是在50 。 现在我们所要做的就是像使用那样使用.StartUpPosition = 2

 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 

由Chris Neilsen提供(来自评论)

你也可以使用:

 VBComp.Properties.Item("StartUpPosition") = 2