Solved

VBA: Saving data in a userForm

Posted on 2000-02-23
6
1,071 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Have you ever had the experience that you had to follow 10 steps over and over again every time when you need to nicely forward an important email to your manager? Fear no more! With the help of the Quick Steps feature in Outlook 2010, your old chor…
Introduction It seems that at least a couple of times per month, I answer a question that requires automating Outlook from another Microsoft Office application, usually (although not always) to send one or more email messages.  For example: …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

747 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

9 Experts available now in Live!

Get 1:1 Help Now