Consulting

Results 1 to 6 of 6

Thread: Updating Userform Captions with VBA - Word

  1. #1

    Updating Userform Captions with VBA - Word

    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.

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  3. #3
    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.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Last edited by gmaxey; 02-21-2014 at 10:04 AM.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    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

  6. #6
    gmaxey, Perfect. Works like a charm. Thank you!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •