Consulting

Results 1 to 5 of 5

Thread: Save Form With Data

  1. #1
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    2
    Location

    Question Save Form With Data

    New user here... Forgive my ignorance but I've read http://www.vbaexpress.com/forum/show...form-with-data a few times and haven't been able to figure out how to apply it to my macro.

    My situation is that I have a macro with a lot of fields (~40) that are input by the user, such as names, addresses, phone numbers and others. From that data, using a command button and a bunch of code (a few thousand lines,) the macro generates word documents based on the information input into the form by the user. What I want to do is for the macro to save the data (somewhere/somehow) that has been input so that, when the form closes, if a user finds out that they need to change 1 field, they don't have to enter everything again; they can re-load the macro, with the data that was input previously and just change the one field and re-generate the documents.

    Can anyone shed some light on how to save all the data when the macro closes so it can be accessed next time it is run?
    Last edited by macropod; 06-27-2019 at 07:20 PM.

  2. #2
    VBAX Regular
    Joined
    Jun 2019
    Location
    Wellington
    Posts
    8
    Location
    G'Day

    This is fairly standard fare. You can do it a number of ways, if you are using Bookmarks then presumably your User Form can be loaded using the values from the Bookmarks in the document. It's easy to retrieve their value:

    Dim valueInDocument as String
    valueInDocument = ActiveDocument.Bookmarks("myBookmark_1").Range.Text

    If for some reason you can't reload it from the Bookmarks, you could try persisting your data as Document Variables (ActiveDocument.Variables), and displaying their value by updating a Document Variables corresponding Bookmark or by using DocVariable Fields in your document. If you use DocVariable Fields be sure to add some code to update these fields after you have updated the Document Variables value.

    If you are using a User Form to capture the data you are entering, when you redisplay it you can populate it using the values from either the Bookmarks or the Document Variables. If you want to display blank input fields the first time the form is displayed you can always add a custom Property to your form and set it the first time the form is displayed, but not when the form is subsequently displayed. In your User Forms Initialisation event you can test to see if this custom Property is True or not, If it's not True (sorry, False) you can then proceed to load the controls in your form with the values from your document. These will be retrieved from either your Bookmarks or Document Variables, depending on which you have decided to use.

    I hope this helps - Peter

  3. #3
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    2
    Location
    Peter - can you give me some more details on how to code this? I'm not familiar with bookmarks nor how to save the Document Variables so that they are kept for next time.

    Thank you.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    You might find this helpful: https://gregmaxey.com/word_tip_pages...userforms.html

    Best Regards,
    Greg Maxey

    A politician thinks of the next election; a statesman thinks of the next generation. ~James Freeman
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Jun 2019
    Location
    Wellington
    Posts
    8
    Location
    jhandler, if you still need further help after reading Gregs article, reply again and we'll provide some more specifics.

    Cheers - Peter
    Inner Word Limited, Wellington, New Zealand

Posting Permissions

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