Solved

VBA: Saving data in a userForm

Posted on 2000-02-23
6
1,193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
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…

696 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