Consulting

Results 1 to 7 of 7

Thread: How to make Word userform retain previous input info when doc is closed then reopene

  1. #1

    How to make Word userform retain previous input info when doc is closed then reopene

    Hi

    I have created a template (.dotm) in Word 2011 which contains a user form.

    The user form pops up when someone opens the template (ie a new document is created based on the template) which is great.

    The user inputs the information and the document is automatically populated with the information. All good so far.

    I have created a macro and assigned it to the QAT so when the user wants to change any of the information contained in the user form they click on the button and the user form pops up. The previously inputted information is still there which is exactly what I want as most people only want to change one or two bits of information.

    When the document is saved and closed, then reopened, the information in the user form is lost. That is, when the user clicks on the macro to update the user form, the form pops up but it's blank. The means the user has to input all of the information again, which causes frustration.

    Is there a way of coding the form so that the last set of information is displayed every time the user form called up, even when the form has been closed and re-opened?

    I have attached a copy of the code for the user form.

    I hope someone out there can help me!

    Dawn

    PS - I'm new to code so if you could keep the explanation as simple as possible I would be very grateful
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Dawn,

    To re-populate the userform, you could simply read in the bookmark values upon opening. Perhaps a better approach would be to use the userform code to populate a set of document variables, then use DocVariable fields in the body of the document wherever you want the data replicated, plus and 'ActiveDocument.Fields.Update' line in your code. This has a number of advantages, including not needing fragile bookmarks and the ability to replicate the same item as many times as necessary from a single DocVariable. To re-populate the userform, you could then simply read in the relevant document variable values upon opening.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Hi Paul

    Thank you for your reply.

    I'd like to use the document variable approach, but have no idea where to start.

    If you have time, would you mind giving me an example of how the code should look?

    Regards

    Dawn

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Dawn,

    For a great deal of useful stuff for implementing userforms (including the use of DocVariables), See: http://gregmaxey.mvps.org/Create_and...a_UserForm.htm
    One thing that doesn't seem to be covered there is how to re-populate the userform upon re-opening the document. To do that, your userform initialization code for, say, a phone # could include something like:
    Me.txtPhone.Value = ActiveDocument.Variables.Item("varPhone").Value
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Hi Paul

    I have decided to stay with the fragile bookmarks for the time being. Once I get a better undrestanding of VBA I will look at moving across to doc variables.

    Thanks for your sample code - my user form is now reading the bookmarks and displaying the text every time the user form is opened.

    There's one more thing I'd like help with if you have the time?

    When someone opens the template and gets a document based on that template, they enter the information into the user form then ultimately save the document and close. When they re-open the document, I don't want the user form to open automatically, just when it is called via the macro.

    Is this possible?

    I'm thinking it might be something to do with looking up the file name and if it is the same as the template to display the user form. If it is different to the template, don't display the form?

    That's as far as my thought process has gone and I don't know how to transfer that thought process into code, so any help would be greatly appreciated.

    Dawn

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,341
    Location
    Paul,

    That stuff is covered here: http://gregmaxey.mvps.org/Interactive_UserForms.htm
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Thanks Greg.

    Dawn: I suspect the issue has to do with how you're calling the userform when the document is created/opened. Without seeing the code you're using for that, I can't tell you what changes you might need to make.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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