Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 882
Ryszard Jędraszyk Avatar asked May 29 '18 11:05

Ryszard Jędraszyk


1 Answers

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

like image 140
Ahmed AU Avatar answered Nov 07 '22 15:11

Ahmed AU