Solved

VBA: Saving data in a userForm

Posted on 2000-02-23
6
1,091 Views
Last Modified: 2009-07-29
I've noticed that you can save data in controls placed directly on a word document: i.e. text placed in a textBox control on a word document is saved along with the document so that it is still there when the document is reopened.  Like regular Visual Basic, data put into a control on a userForm aparently is not saved when the form is unloaded: i.e. the default properties of the control are set when the form is reloaded regardless of what the user put into the control earlier.  Is there any way to save the inputs on a user form without creating a separate data file or putting the data on the word document itself?  If a separate data file is required, is there any way to encapsulate this data with the word document so the user sees just one (word document) when opening the project?
0
Comment
Question by:SteveW
6 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 2551541
Hi SteveW,

This is for a seperate textfile form where you can load and save data for the formproperties...

http://support.microsoft.com/support/kb/articles/Q197/0/21.ASP?LNG=ENG&SA=ALLKB&FR=0

But you mentioned this was not what you were looking for...

Leaves another possibility open, that of the bookmarks. You could put some hidden bookmarks into the document, where you store data for formfields, and retrieve them from.

If you would like a sample, i'll write one...

HTH:O)Bruintje
0
 
LVL 22

Expert Comment

by:ture
ID: 2553694
SteveW,

Just an idea...

If it's only a few values you wish to remember, and if the values should be saved for the current user/machine, you might find it easy enough to use the registry.

The parameters (all strings) are:
Application, Section, Key and Setting (only when setting the value). Here's an example:

'Write to registry
n = "Ture Magnusson",
SaveSetting "TuredataInvoicing", "OrderForm", "CustomerName", n

'Read from registry
n = GetSetting("TuredataInvoicing", "OrderForm", "CustomerName")

Ture Magnusson
Karlstad, Sweden
0
 
LVL 5

Expert Comment

by:vboukhar
ID: 2564314
Other way - to store your information into custom document properties (like data in TextBox1 in my example):

Private Sub UserForm_Initialize()
On Error Resume Next
' if custom property doesn't exist yet
Me.TextBox1.Text = ThisDocument.CustomDocumentProperties("TB1")
On Error GoTo 0
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
On Error Resume Next
ThisDocument.CustomDocumentProperties.Add "TB1", False, msoPropertyTypeString, Me.TextBox1.Text
ThisDocument.CustomDocumentProperties("TB1") = Me.TextBox1.Text
On Error GoTo 0
End Sub

Hope it helps.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:ture
ID: 2615528
SteveW,

Even better than CustomDocumentProperties is to use document variables. They can contain more data (up to ~60 K strings) and they don't show up when right-clicking a document to see it's properties.

Sub UseDocumentVariables()
  'Create new variable if not already present
  On Error Resume Next
  ActiveDocument.Variables.Add Name:="invoicenumber"
  On Error GoTo 0
 
  'Set value of variable
  ActiveDocument.Variables("invoicenumber").Value = 36500
 
  'Display value of variable
  MsgBox ActiveDocument.Variables("invoicenumber").Value
End Sub

/Ture
0
 
LVL 22

Accepted Solution

by:
ture earned 200 total points
ID: 2615545
Upgrading to answer.
0
 

Expert Comment

by:BegnrSteve
ID: 2628491
Maybe, I don't understand the original question corrrectly, but it sounds as if SteveW was asking about templates which is an easy and completely successful way for the user to use the form and input data to be saved as a document for later retrieval and editing or whatever.  Maybe, SteveW will come back to tell me that this is not an option.  

You folks have provided sufficient answers to his question and the solutions are certainly excellent.  Good discussion here.   ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Formatting test 26 61
how can I correct in impaired MS Word 2013 invoice template print out? 8 51
PowerPoint Links 7 37
Excel 2016 - Black cell borders 11 26
Introduction Perhaps more familiar to developers who primarily use VBScript than to developers who tend to work only with Microsoft Office and Visual Basic for Applications (VBA), the Dictionary is a powerful and versatile class, and is useful …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now