 |
Search
IT Courses |
|
|
|
|
|
|
|
|
|
|
|
IT
dart News Letter
|
Get
ITdart.com weekly headlines before it's published on our site! Subscribe
and receive the articles delivered to your inbox!
|
|
|
Storing variables
This is a very interesting and useful feature. On many occasions, you may want to store some values while some code runs in a document. You want these values to be usable even after the document is closed and reopened. This would normally require storing the values in a database because the VBA variables exist only in memory.
This is possible using a built-in feature of Word which allows you to define named variables and store values in them on a persistent basis. When you save the document and close it the values are also stored along with it. When you reopen the document the past values are available to you programmatically.
Here is how to do it:
There is a collection called Variables in the document object of Word. Therefore to add a new variable to it use the following syntax:
activedocument.variables.add "var name", "value" Now if you close and reopen the file, you can retrieve the value as follows:
?activedocument.variables("nitin").value You could also use other approaches to store values across document editing sessions:
You could use the registry using the privateprofilestring property. This method is good if the value is to be stored in a machine specific manner. If the value to be stored is specific to the document, this method is not useful. Use Autotext entries in the word document. Use Input/Output statements to store the values to an external text file.
|
|
|