Link to home
Start Free TrialLog in
Avatar of Denis Welch
Denis WelchFlag for United States of America

asked on

Change the name of a VBA form programmically.

I'm using VBA to create a UserForm with controls on it, That all works, however each UserForm is created with the name UserForm1, UserForm2, etc. I'm unable to programmically change the UserFrom name using:

Application.ActiveDocument.VBProject.VBComponents("UserForm1").Name = "ufMainColorForm"

I get the following error "Run-time error 75: Path/File access error"

The "Name" property seems to be read-only, i.e., I can list all the form names, just can't change any of them except manually through the IDE.

Above example is from MS Word, but Excel shows the same problem.

Any suggestions would be much appreciated.

Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

You can rename it whilst it is being created, something like this

''///Make a userform
Set MyUserForm = ActiveWorkbook.VBProject _
      .VBComponents.Add(vbext_ct_MSForm)
      With MyUserForm
            .Properties("Height") = 100
            .Properties("Width") = 200
            On Error Resume Next
            .Name = "MyNewForm"
            .Properties("Caption") = "UserForm name is  " & .Name
      End With
      

Open in new window

Userforms have a Tag property and you can probably put a different value like the form's "name" or a sequential number in each one.

In Excel, you will get a runtime error saying that access to the VBProject isn't trusted. The cure is to check the box to Trust access to the VBA project object model in the File...Options...Trust Center...Trust Center Settings...Macro Settings ribbon item. Word has the same checkbox.


Having done that, you may then use code in Excel like shown below to rename UserForm1:

Sub RenameUserForm()
Application.ActiveWorkbook.VBProject.VBComponents("UserForm1").Name = "ufMainColorForm"
End Sub

Open in new window

User generated image


One of the best guides to was written by Chip Pearson Programming The VBA Editor

I have created a small example for you adding a UserForm with a button and combobox adapted form one of my projects.
MakeUserForm.xlsm

You won't be able to rename the form while it is loaded and would get an access error.

Avatar of Denis Welch

ASKER

Thanks Roy, but that approach didn't work. I'm still only able to change the form name manually.
Rory, that seems to be true to even change the name manually. I had to close the file and re-open and then I was able to rename the form manually when the form was created programmatically. This is not an issue if the form is created manually within the IDE.
Thanks
Roy,

That wasn't my issue. I'm able to create a form and populate with controls without issue, programmatically. The only issue was programmatically changing the form name. Initially, the form name is UserForm1. I want to change the name to something more descriptive as I have multiple forms.

Yes, Chip's site is terrific.

Thanks
byundt, I tried your suggestion, but it did not change the form name. Right now, I'm just going to have to keep track and change the form name manually. For this application that will work.

Thanks, Denis
Thanks Martin, but using the tag property doesn't solve my issue. To add controls to the form, I have reference the form name.
byundt, sorry forgot to mention your other recommendation regarding macro security. I had already done that.
Thanks Martin, but using the tag property doesn't solve my issue. To add controls to the form, I have reference the form name.
Try something like this:
    Dim frm As Object

    For Each frm In VBA.UserForms
        If frm.Tag = "Whatever" Then
           Msgbox frm.name
        End If
    Next

Open in new window

Thanks Martin, that is a workaround. I was hoping that I could change the form name itself. Otherwise, what shows up in the Project Explorer is UserForm1, UserForm2, UserForm3....Since I'm unable to find a way to programmatically change the form name, I'll just do it manually for this application.

I don't understand why it is not renaming the form, it certainly does when run the code.
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for everyone's help. I was able to create a form, populate it with controls, and add code. All worked fine. Just couldn't change the name of the form. Cleaned out all temp files on computer (Win 10 Pro), rebooted. Now changing the form name programmatically works using same code that didn't work prior to reboot.

Again, thanks for everyone's help.

Denis