UserForm error: Method 'Properties' of object '_VBComponent' failed

本秂侑毒 提交于 2020-01-02 04:31:13

问题


I want to edit my UserForm object in VBE with code.

When I use the following, I get an error as in post title:

Sub myTest()
    ThisWorkbook.VBProject.VBComponents("UserForm1").Properties("Caption") = "myCaption"
End Sub

I tried changing "Caption" to "caption", the same error. Also this error happens when I try to set Width or Height properties, not always at first time, but when I run the code again to resize.

Edit 1: "Trust access to the VBA project object model" in macro settings is enabled.

Edit 2: Error does not happen when new workbook and UserForm are created. It does happen when UserForm is recreated in workbook in which an error already happened.

Edit 3: Adding Unload UserForm1 or Set UserForm1 = Nothing before doesn't help.

Edit 4: For new workbook, if I create UserForm and run this code from a module, but after right clicking on UserForm -> View Object, then I get an error: "Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus." When I confirm, on consecutive macro runs I'm getting an error as in post title.

Edit 5: The same happens on Excel 2010 and 2016.


回答1:


May simply try this

Sub myTest()
 ThisWorkbook.VBProject.VBComponents("UserForm1").Activate
 ThisWorkbook.VBProject.VBComponents("UserForm1").Properties("Caption") = "myCaption"
End Sub

It is working in Excel 2007 and expect to work in any version. May Please refer my Answer to Post After Import of a Userform into VBComponents Properties cannot be read



来源:https://stackoverflow.com/questions/50582890/userform-error-method-properties-of-object-vbcomponent-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!