PDA

View Full Version : Saving Userfoms' values to text file and load the data back to userforms



Dubravak
08-25-2015, 12:59 AM
Hi everyone,

I am working on a word-userform-based application :) User will use just userform for data entry (no access to document at all) and finally it will be possible to print out the document, or export as pdf. But also I need to figure out exit and save button (if user could not finish the process). Is there any simple way how to save all userforms (6) data with all controls state (textboxes, comboboxes, optionbuttons) to text file and also how load this data to userform back, when the application starts.

Thanks

gmayor
08-25-2015, 05:22 AM
If you write the values to bookmarks or docvariables in the document under preparation, it is easy enough to read back the values in the bookmarks or docvariables into the elements of the userform when the document (or the userform) is next opened.

Imagine a simple userform with a textbox a checkbox and a combo box with two command buttons - one for cancel one to continue, all using the default names. The userform code would be

Option Explicit

Private Sub CommandButton1_Click() 'OK
Me.Tag = 1
Me.Hide
End Sub

Private Sub CommandButton2_Click() 'Cancel
Me.Tag = 0
Me.Hide
End Sub


Then the macro that calls and processes the userform could be as follows:


Option Explicit

Sub RunProcess()
Dim oDoc As Document
Dim oVar As Variable
Dim bVar As Boolean
Dim oFrm As New UserForm1
Set oDoc = ActiveDocument
With oFrm
For Each oVar In oDoc.Variables
If oVar.name = "var1" Then
.TextBox1.Text = oVar.Value
Exit For
End If
Next oVar
For Each oVar In oDoc.Variables
If oVar.name = "var2" Then
.CheckBox1.Value = oVar.Value
Exit For
End If
Next oVar
With .ComboBox1
.AddItem "Select Item"
.AddItem "Item A"
.AddItem "Item B"
.AddItem "Item D"
.AddItem "Item E"
.ListIndex = 0
End With
For Each oVar In oDoc.Variables
If oVar.name = "var3" Then
.ComboBox1.ListIndex = oVar.Value
Exit For
End If
Next oVar
.Show
If .Tag = 0 Then GoTo lbl_Exit 'cancel selected
If Not .TextBox1.Text = "" Then 'You can't store nothing in a docvariable
oDoc.Variables("var1").Value = .TextBox1.Text
End If
oDoc.Variables("var2").Value = .CheckBox1.Value
oDoc.Variables("var3").Value = .ComboBox1.ListIndex
End With
Unload oFrm
lbl_Exit:
Set oDoc = Nothing
Set oVar = Nothing
Exit Sub
End Sub


This covers the main elements on the form and shows how to write the values to document variables and read the values into the form (if present) when the form is opened or reopened. You can scale this up and use more meaningful names to suit your process.

The values can be displayed in the document itself using docvariable fields. You can use the code at http://www.gmayor.com/installing_macro.htm to update the fields.

SamT
08-25-2015, 07:05 AM
UserForm Applications grow. There is no "easy" way to save data from all possible Draft files as text files.

Personally, I would create a "Drafts" folder in one of the User's folders in "C:\Documents And Settings\UserName\[Your Application name]\Drafts" and just save the Document there for later retrieval. Another possibility is "C:\Program Files\Microsoft Office\[Your Application name]\Drafts\UserName".

Also consider "C:\Program Files\[Your Application name]\," and under that folder: "Drafts\," "Pending\," and "Archive\," each with UserName folders under them.

The possibilities are almost endless; Just use your imagination. BTW, where do you want to store the Template Docs used in your Application