PDA

View Full Version : [SOLVED:] Updating Userform Captions with VBA - Word



jgoodrich
02-20-2014, 02:48 PM
Hello All,

I have tried searching, but cannot find the answer. I'd like to set-up a macro that when run will update the captions of labels within a userform PERMANENTLY using text that is inside a form field.

I set this up:

userformname.labelname.caption = formfield.result. This works when you open the form while it is protected, but it's not truly changed in VBA when I go to look, therefore not permanent.

Hopefully someone will have a recommendation. Thank you.

fumei
02-20-2014, 07:24 PM
Huh? I am not following that. When does that code run? "Open the form while it is protected". What do you mean by that? Do you mean show the userform when the document is protected versus showing the userform when it is NOT protected. Why and when is it being Unprotected?

In any case if the userform s NOT shown, captions will be whatever is setup in the userform itself. If you need to change things (and why are you????), build logic to cover that in the Userform_Initialise event.

jgoodrich
02-21-2014, 06:00 AM
Fumei, sorry for the confusion.
The code will run on command as a way to update the captions to whatever was typed in respective form fields.

What I was saying is that changing the caption with that code works if right after I add userform.show. However, if I return to VBA, the captions were not permanently updated, and they returned to whatever they were before the macro ran. Changing the captions in VBA isn't what I'm looking for. I'd like to have it work directly from the document for any user to update captions. Such as someone that has no VBA skills and wants to customize their labels to the template I am creating.

gmaxey
02-21-2014, 09:48 AM
If you want to make permanent changes to userform control properties then you will need to use the designer:

e.g.,:
Sub Change_Userform()
ThisDocument.VBProject.VBComponents("Userform1").Designer.Controls("Label1").Caption = "Some new caption text"
End Sub

You will need a reference to Microsoft Visual Basic for Applications Extensibility

westconn1
02-23-2014, 02:25 AM
you would need to put some button on the toolbar or ribbon, for the user to press, assign a macro as above, probably add code to make sure the form is protected to get the correct value as the new caption

jgoodrich
02-24-2014, 10:35 AM
gmaxey, Perfect. Works like a charm. Thank you!!