PDA

View Full Version : Reloading Userform after initial submission



ajhez
05-25-2015, 07:07 AM
Hi all,

I wondered if it was possible to reload or bring back a Userform once it has already been used.

For example, a Userform to create a letter might populate bookmarked sections of the letter with the text inputted into the various TextBoxes in the Userform. Often once the user has completed the Userform they press 'Submit' or 'OK' via a commandbutton, the letter is generated with the information selected / inputted in the Userform and the Userform is hidden.

However, where the user then notices mistakes or wants to make changes for whatever reason, would it be possible to 'reload' the previous Userform (with the users previously selected/inputted information) so that it could be altered and then resubmitted? The point being that much of the previous information will be fine and only a handful of things may need to be changed, so keeping the previous information to be amended in the Userform would save the user time rather than re-inputting good information.

I imagine if this is possible, it would potentially require the initial round of changes made by the first submission of the Userform to be undone?

Any thoughts would be great! :help

Thanks,
AJHEZ

ajhez
05-25-2015, 08:13 AM
I probably should include that I start the Userform upon opening a new document with:


Private Sub Document_New()
UserForm1.Show
End Sub


And once the User decided to submit the Userform by pressing the commandbutton after the various code to deal with the inputting, I use:


UserForm1.Hide

gmaxey
05-25-2015, 10:00 AM
Have you tried Userform1.Show?

Edited: Sorry it seems I didn't fully read you post/question. I describe your situation as needing and interactive userform. See:
http://gregmaxey.mvps.org/word_tip_pages/interactive_userforms.html

Paul_Hossler
05-25-2015, 10:22 AM
Inside Document_Open() you could read the Bookmarked data into the UF controls, allow the user to make changes and an [Update] or [Discard] option

gmayor
05-26-2015, 02:19 AM
At its most basic - lets assume a userform with three text boxes and a command button. Add the following to an ordinary module in the template. This will look at the bookmarks in the document and will fill the named form text boxes with the values of the three named bookmarks. When the command button is clicked the text box values are written back to the bookmarks.



Option Explicit

Sub ProcessForm()
Dim ofrm As New UserForm1
Dim oBm As Bookmark
With ofrm
For Each oBm In ActiveDocument.Bookmarks
Select Case oBm.Name
Case "bm1": .TextBox1.Text = oBm.Range.Text
Case "bm2": .TextBox2.Text = oBm.Range.Text
Case "bm3": .TextBox3.Text = oBm.Range.Text
End Select
Next oBm
.Show
If .Tag = 1 Then
FillBM "bm1", .TextBox1.Text
FillBM "bm2", .TextBox2.Text
FillBM "bm3", .TextBox3.Text
End If
End With
Unload ofrm
lbl_Exit:
Set ofrm = Nothing
Set oBm = Nothing
Exit Sub
End Sub

Private Sub FillBM(strBMName As String, strValue As String)
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strBMName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub



The command button code will be


Private Sub CommandButton1_Click()
Me.Hide
Me.Tag = 1
lbl_Exit:
Exit Sub
End Sub



You can call the main macro 'ProcessForm' from both a Document_Open and a Document_New macro (and a button on the QAT or ribbon also if you wish).
If the bookmarks don't exist nothing will happen to the document or form in respect of the missing bookmark(s).

ajhez
05-27-2015, 08:38 AM
I had tried UserForm1.Show, but this didnt appear to do anything at all.

I will take a look at gmayors idea and give it a whirl - I have a huge amount of textboxes in the userform so i'll need to knuckle down to get through it all.

Cheers for the suggestions as always!

SamT
05-27-2015, 10:55 AM
You might try a UserForm2, with just a CommandButton and sized to fit the CBut. The CBut Form must have its ShowModal Property set to False so the User can interact with the document as needed.

Use the CBut form's Terminate Event Or use two buttons to actually submit the document, and UserForm1's "Submit" button to show the CBut form and display the new Doc.

In the attached SamTexample.doc, run the Macro "TestSamT." Input some text in the TextBox on the Form that Shows, then click the CBut.

gmaxey
05-27-2015, 02:41 PM
All spelled out for you here: http://gregmaxey.mvps.org/word_tip_p...userforms.html (http://gregmaxey.mvps.org/word_tip_pages/interactive_userforms.html)