Naming Dynamically Created Controls on Imported Userforms

CodeMonkey01

New Member
Joined
Jul 21, 2011
Messages
6
Note: I am unable to reply to my own thread to bump it at work, therefore I have reposted (as it had zero replies) and deleted my original thread. EDIT: I thought I could delete my own post, but I cannot - my apologies to the forum and the moderators for misunderstanding that the edit post was a time-limited feature.

A colleague and I are building a template builder, which reads data from an XML, opens a new workbook, imports a number of pre-coded modules and userform(s), then dynamically adds buttons and labels to the userform, complete with event handlers. We've gotten 99% of it to work, but we cannot name our dynamically-created controls. For instance:

Code:
With wbProject.VBProject.VBComponents
        '--Load the list of nodes we want to loop through
        Set ufMain = .Item("ufMain")
        Set frmSummary = ufMain.Designer.Controls.Item("frmSummary")
 
        Set xmlNodeList = JOBXML.SelectNodes("/JobProcessingInstructions/LoanHeaders/LoanHeader")
        For Each xmlNode In xmlNodeList
            '--Retrieve the label value from the xml
            strLoanHeader = xmlNode.Attributes.getNamedItem("Value").nodeTypedValue
            Set lblHeading = frmSummary.Controls.Add("Forms.Label.1") ', "lbl" & strLoanHeader & "Heading")
            With lblHeading
                .Name = "lbl" & strLoanHeader & "Heading"
                .Top = 12 + (iRows * 18)

The compiler blows up on the .name line with this message:

Run-time error '40044':

Could not set the Name property. Could not complete the operation due to error 800a9c6c.

Google has failed me in regard to that error number (800a9c6c). Thus far, I have been unable to use ufMain.Controls(lblHeading.Name).Properties("Name") = TextString, either, as the compiler says the object doesn't support this property or method.

Does anyone know how I can set a name for my controls that are created this way? We've determined workarounds for this, but it's going to be a big pain and may not work in the future; this project builder is intended to be available to non-programmers who can edit an XML in the future.

I am working in Excel 2007.

Thank you in advance for any assistance.
<!-- / message -->
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I'd guess the control name is invalid - e.g. it contains a space.
 
Upvote 0
Aren't you naming the control twice?

Once when you create it and then again on the line you say causes the error.

No idea if that's the problem, it is more likely to be an invalid name as Rory suggests.

By the way why would you use Properties("Name") instead of just Name?
 
Upvote 0
It is hard to see in the code box, but I commented out the part where I named it on creation <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Code:
[COLOR=black][FONT=Verdana]("Forms.Label.1") ', "lbl" & strHeader & "Heading"[/FONT][/COLOR]
<o:p></o:p>
<o:p></o:p>
I left that in to show I had already tried that method of naming (I got the same weird, un-Google-able error). <o:p></o:p>
<o:p></o:p>
I tried the Property("Name") method just as a last resort - it doesn't exist.
The name of the variable has no spaces or special characters. In fact, the name is 'Note' for the first instance where it blows up.
 
Upvote 0
I take it you've double checked strLoanHeader when the code errors?

What exactly did you search for?

I found quite a few results for '40044', some about creating userforms in similar ways.

Where they not relevant?

They mentioned type mismatch quite a bit and there were a few mentions of late/early binding.

Perhaps worth a look?
 
Upvote 0
Your code works for me with a valid name, and fails with the error you mention if I pass an invalid name (I suspect that would include a name already in use)
 
Upvote 0
CodeMonkey

When do you get the error?

I found another similar thread on the forum and the problem appeared to be because the OP was in debug mode when running the code.
 
Upvote 0
Apologies for the late response: my work only now unlocked the ability to post to a message board.

I get the error at run-time when the imported userform is having controls added to it. I'm not familiar with a debug mode in Excel 2007, so if you have insight into that issue, I would appreciate your guidance.

Re: Type Mismatch on Error 40044 - doesn't apply because I'm assigning a string to the .Name variable. Those searches did not seem to apply to what I'm doing.

This may be a non-issue at present, as we've built in a workaround to this issue.

@rorya: Are you importing the userform at runtime, then adding the control and naming it? The userform (and the code modules) are all imported at run-time, not design time.
 
Upvote 0
CodeMonkey

The type mismatch stuff I found weren't really anything to do with the Name property.

It was more to do with the way the controls were being created, if that makes sense.
 
Upvote 0
Is there a reason that you are leaving the initial With open?
It looks like after the Set ufMain, the With is not used.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,749
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top