Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2006

Tip: Looking for answers? Try searching our database.

Remembering values between instances

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cory - 26 Dec 2006 16:58 GMT
My problem is as follows:
   I am creating a form template that first asks the user to personalize
the form (since it hase several inputs that will never change for that user)
and then instructs the user to save the resulting document as a template.  
But I want that template to run a seperate autonew.  If I supplied an
If...Then statement at the begining I could just keep the Autonew that I have
and let the user Personalize then have a variable set and bypass the rest of
the instructions.  When they save the template and open it the next time, the
value could be read executing the Then portion and bypassing the
personalizing  bit.

So...  How do you set a variable value in the code that will be read the
next time the document is opened?  As far as I am aware, the variables are
not persistent.  Of course keep in mind I only know enough about this to get
myself in trouble.

Cory
Helmut Weber - 26 Dec 2006 22:18 GMT
Hi Cory,

>So...  How do you set a variable value in the code that will be read the
>next time the document is opened?  As far as I am aware, the variables are
>not persistent.  Of course keep in mind I only know enough about this to get
>myself in trouble.

you got to store the values somewhere,
in the registry, in a txt-file,
in a document-variable,
in a document-property,
be it custom or built-in.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Cory - 27 Dec 2006 01:32 GMT
Alright, since I (and the other users) am/are on a regular user account, the
registry is out of the question, as far as usability, the text file is too.  
How do you set a document variable or property as the value, and what are the
pros/cons of each?

Cory

> Hi Cory,
>
[quoted text clipped - 8 lines]
> in a document-property,
> be it custom or built-in.
Jay Freedman - 27 Dec 2006 05:12 GMT
Document variables and document properties are very similar. Both are
stored as part of the document's file, so they stick around through
closing and reopening the document and/or Word. There are three main
differences:

- A document variable can be created or altered only by VBA code,
while a document property can be created or altered by the user in the
Custom tab of the File > Properties dialog.

- A document variable is always stored as a string, while a document
property can be given a data type of string, number (integer), float,
date, or boolean (true/false). In practice, the conversions between
strings and the other types are mostly automatic.

- Setting a string-type document property to an empty string ("") just
makes it an empty string, while setting a document variable to an
empty string deletes the variable (so trying to get its value after
that results in an error).

To create a document variable, you can just assign a value to a
previously unused name like this:

Sub CreateVariable()
   Dim myString As String
   myString = "Hello World"
   ActiveDocument.Variables("NameOfVariable") = myString
End Sub

You can use the ActiveDocument.Variables.Add method, but it doesn't do
anything this simple assignment doesn't do. Besides, if you try to use
the .Add method with a variable that already exists, you get an error;
but the simple assignment just changes the value.

To read back its value, you can insert a DocVariable field in the
document:
   {DocVariable NameOfVariable}
or you can write code that uses its .Value member:

Sub ReadVariable()
   MsgBox ActiveDocument.Variables("NameOfVariable").Value
End Sub

To create a document property, you must use the .Add method of the
CustomDocumentProperties collection:

Sub CreateProperty()
   Dim myString As String
   myString = "Hello World"
   ActiveDocument.CustomDocumentProperties.Add _
       Name:="NameOfProperty", _
       LinkToContent:=False, _
       Value:=myString, _
       Type:=msoPropertyTypeString
End Sub

(To store one of the other data types, change the Type parameter.) The
kind of assignment that works to create a new document variable
doesn't work for document properties. :-(  But you can change the
value of an existing property by assigning a new .Value to it.

To read back its value, you can insert a DocProperty field in the
document:
   {DocProperty NameOfProperty}
or you can write code that uses its .Value member:

Sub ReadProperty()
   MsgBox ActiveDocument _
       .CustomDocumentProperties("NameOfProperty").Value
End Sub

There are a few other differences, but they're more esoteric -- for
example, I think custom document properties can be read without
opening the document by using dsofile.dll (see
http://www.word.mvps.org/FAQs/MacrosVBA/DSOFile.htm).

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org

>Alright, since I (and the other users) am/are on a regular user account, the
>registry is out of the question, as far as usability, the text file is too.  
[quoted text clipped - 15 lines]
>> in a document-property,
>> be it custom or built-in.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.