storing value in registry

A

Al

Hi guys,

I have a listbox and want the default value of the listbox to be the same
that was last used.

ie: if the user picked option "5" then next time that template is run I want
the default to be option "5".

I guess the best way would be to store the defualt value in the registry.
Anyone know how to do this?

Cheers,

-Al
 
P

Peter Hewett

Hi Al

The best way to do this is not to use the registry but to use a document
variable. Document variables are stored with the document. To create or
overwrite an existing Document variable use:

ActiveDocument.Variables("DocVar Name") = "DocVar Value"

to retrieve a value use:

Dim strValue as string
strValue = ActiveDocument.Variables("DocVar Name")

HTH + Cheers - Peter
 
J

Jezebel

Peter's right that you should avoid the registry if you can, but using a
document variable is only going to work if you've always got the same
document open when you're displaying the listbox.

The VBA functions for using the registry are GetSetting() and SaveSetting().
Well explained in Help.
 
P

Perry

Adding Doc variables and storing the last utilised listbox listindex in
a document variable, causes Word to prompt users to save the template.

In certain cases, you don't want this.

If so, use the registry instead (see Jezebel's proposal) or use a plain
ini file to store these values.

Example of the latter:
When closing the userform, do something like

System.PrivateProfileString(ThisDocument.Path & "\Listbox.INI", "ListBox1",
"ListIndex") = _
Me.ListBox1.ListIndex

When loading the userform, do the reverse of above statement.

More info
http://msdn.microsoft.com/library/d...us/vbawd10/html/woproPrivateProfileString.asp

Krgrds,
Perry
 
J

Jean-Guy Marcil

Adding Doc variables and storing the last utilised listbox listindex in
a document variable, causes Word to prompt users to save the template.

Just add a

AttachedTemplate.Save

statement just after changing the doc variable.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Peter Hewett

Hi Perry

What are the circumstances that adding a document variable to a document
causes it to dirty the attached template?

Cheers - Peter
 
P

Perry

Sure.
But using an INI file thus not having to go through the save process
*and* considering the impatience of users (nowadays), I tend to
favour the usage of an INI file.

Perry
 
P

Perry

If the doc variable uses the template as basis, changing it's value
will cause the template to go dirty.

Why am I explaining this to you, Peter? ... <g>

Krgrds,
Perry
 
J

Jean-Guy Marcil

Very true,

But, you have to make sure that the user carries the INI file along if they
move the file around the network or e-mail it... Or is there a way around
that?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Peter Hewett

Hi Perry

Not for me it doesn't!! I've tried producing a dirty template by updating a
DV in a document, I've been unable to get Word to prompt me to save changes
to the template, only ever the document. I tried using Word 2k and XP but no
luck.

I created a template with a simple user form with 2 buttons:

Private Sub btnCancel_Click()
Me.hide
End Sub

Private Sub btnDirty_Click()
ActiveDocument.Variables("My Test").Value = _
"This is crap" & Int(Rnd * 1000)
End Sub

Plus code in the ThisDocument class to load the form.

Create and save a clean document based on the template (just cancel the
form). Open document click "Dirty", click cancel. Then either save the
document or just exit and allow Word to prompt me "Do I wanna save....".

No dirty template.

Cheers - Peter
 
J

Jezebel

Last selection from a drop-down is a normally a user-preference; in most
cases it's reasonable to assume that the user is machine-specific.
 
J

Jezebel

Word is quirky with this. With a document, just querying a custom document
property dirties the document; but with a template setting the property does
not. Sounds like DocVariables work the same way.

You have to set .Saved to false yourself, then save it, if you want the
values saved.
 
P

Peter Hewett

I modifed the template to display the value of a single CDP stored in the
document. I still don't get a dirty template. I also tried a variant where
I display the value of a CDP in the documents attached template. Still no
dirty template.

Mabe we just construe quirky to mean inconsistent and idiosyncratic where
Word is concerned!

Cheers - Peter
 
J

Jezebel

Inconsistent is exactly right! If you want changes to CDPs or DVs in the
template to be retained you have to dirty the template yourself (ie set
..Saved = FALSE) and then save it.

j
 
J

Jay Freedman

Hi, guys,

I may be crazy to step into the middle of this, but I think there's an
important point to notice: Peter is talking about storing the CDP or
DV in the *document*, and Jezebel is talking about storing them in the
*template*. Each of you is correct *for your location*. Storing the
data in the template will dirty the template, while storing it in the
document will dirty only the document. Does that restore peace, or
just make things worse? :)
 
J

Jezebel

Jay, thanks, but I think Peter and I are actually in heated agreement here.
The conclusion we've both come to is that changing CDPs or DVs in the
template does NOT dirty the template (unlike what happens in the document,
where merely reading them dirties the document). Try it.
 
P

Peter Hewett

I have the following chunk of complex code:

Private Sub btnDirty_Click()

lblDVDoc.Caption = _
ActiveDocument.CustomDocumentProperties("CustomTest").Value
lblDVTemplate.Caption = ActiveDocument.AttachedTemplate. _
CustomDocumentProperties("CustomTest").Value
End Sub

After running this code when I exit the document (manual File>Close), I'm
not prompted to save either the document or the template! So what's getting
dirty and when?

Cheers - Peter


Jay, thanks, but I think Peter and I are actually in heated agreement here.
The conclusion we've both come to is that changing CDPs or DVs in the
template does NOT dirty the template (unlike what happens in the document,
where merely reading them dirties the document). Try it.




Jay Freedman said:
Hi, guys,

I may be crazy to step into the middle of this, but I think there's an
important point to notice: Peter is talking about storing the CDP or
DV in the *document*, and Jezebel is talking about storing them in the
*template*. Each of you is correct *for your location*. Storing the
data in the template will dirty the template, while storing it in the
document will dirty only the document. Does that restore peace, or
just make things worse? :)
Still
cancel
use
use
in
 
J

Jezebel

Odd. Neither of them *should* render the document dirty, but in my VB
add-in, the first line does and the second does not. boh'


Peter Hewett said:
I have the following chunk of complex code:

Private Sub btnDirty_Click()

lblDVDoc.Caption = _
ActiveDocument.CustomDocumentProperties("CustomTest").Value
lblDVTemplate.Caption = ActiveDocument.AttachedTemplate. _
CustomDocumentProperties("CustomTest").Value
End Sub

After running this code when I exit the document (manual File>Close), I'm
not prompted to save either the document or the template! So what's getting
dirty and when?

Cheers - Peter
 
B

Bruce Brown

Jean-Guy's method seemed the easiest but I couldn't make it work.

I added a doc variable to a template and gave it a value of 1.

From that template I then created a document, saved it, and gave the
doc variable a new value of 2 -- followed by the
ActiveDocument.AttachedTemplate.Save line.

When I went back to the template, the doc variable value was still 1.

Peter's doc variable code was missing the .Value property at the end
of the line, which turns out to be unnecessary. Thanks for pointing
out the redundancy.

It will be interesting to see which solution the original poster
chooses.

- Bruce
 
J

Jezebel

Before you save the template, insert this line:

ActiveDocument.AttachedTemplate.Saved = false

Word ignores Save instructions if it thinks the document or template is
saved already. Setting .Saved = false makes the document/template 'dirty'
and thus savable.
 

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