do variables preserve their value after documents are saved?

A

ALBAN

I'm trying to set up a counter that tracks the number of times a certain
button has been clicked. The intention is to preserve the value (after
document is saved and closed) so the counter countinues where it stopped the
last time. Is this possible?

The way I'm doing it at the moment is:

I have created a special template for these documents that need this sort of
counting. In VBA, within the template project I have used the New event to
initialize the counter, hoping that everytime a new document from this
templated is created, it will initialize the counter to 1.
I can use counter correctly for the first time the document is used (the
time it's created by the template).
However, the next time I open the document, the counter is set to a fixed
value!

Any idea why?

thanks in advance,
ALBAN
 
A

ALBAN

Document New event, stored in the template:

Private Sub Document_New()
ThisDocument.Variables("latinChar").Value = 1
End Sub
This is the event that is supposed to do the counting:

Private Sub CommandButton1_Click()
ThisDocument.Variables("latinChar").Value =
ThisDocument.Variables("latinChar").Value + 1
End Sub
The button is in a form that is enabled with a macro that does the following:

Sub InsertCustomFootnotes()
UserForm1.Show
End Sub

According to another posting, variables don't preserve their values between
sessions.
If this is correct, is there another way to achieve what I want to do?

thanks,
ALBAN
 
J

Jonathan West

Ah! You're taking of Document Variables, not code variables.

Document Variables are rather like Custom Document Properties. They are
saved as part of the document and therefore remain between Word sessions
when you re-open the document.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jay Freedman

Yes, document variables are saved in the document.... but use
"ActiveDocument" instead of "ThisDocument".
 
J

Jasonm

ALBAN, sorry to jump in on your thread, but I have a somewhat related
question:

Jonathan, if I were to try this in a document template would it also work?
Right now I am using an external txt file to store my numbers (I have 6
variables that I am incrementing). It could potentially be cleaner to store
them IN the document template if that would work.

I am using my variables to set the filename of the document to increment
document 001, document 002, etc...

any ideas?
Jason Canady
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top