document variables and templates

C

Chip Orange

I'm trying to use document variables with the normaltemplate object, and I
thought I read that variables existed in both templates and documents; yet,
unless I open it as a document I'm unable to use them directly in the
template.

Did I just mis-interpret the documentation on document variables, are they
only available in documents?

thanks.

Chip
 
J

Jean-Guy Marcil

Hi Chip,

Doc Variables can be in both templates and documents.

But, if you create them in a template, and then create a doc based on that
template, your "template doc variables" will also be in the document. I use
that all the time.
For example, sometimes I need to know if a document has been saved at least
once. The first time it is saved, I run a set of macros, and after that a
different set. For that, I create a doc variable in the template with a
value "x", then when the user saves the document, I check the doc variable
value, if it is equal to that of the value in the template - "x" in this
case - (i.e it has not been changed) then this is a first time save. I then
run the "first-time-save" macros and at the end change that doc variable
value to "y", for example. So, the next time the doc gets saved, the doc
variable is now equal to "y", which allows me to run a set of different
macros.
unless I open it as a document I'm unable to use them directly in the
template.
What are you trying to do exactly?

This
'_______________________________________
ScreenUpdating = False
With ActiveDocument.AttachedTemplate.OpenAsDocument
.Variables("UserName") = Application.UserName
.Close SaveChanges:=wdSaveChanges
End With
ScreenUpdating = True
'_______________________________________
will manipulate doc variables in the attached template, but will have no
impact in the active document.

Or this
'_______________________________________
Documents("MyDocument.doc").Variables("UserName") = Application.UserName
'_______________________________________
will manipulate variables in an open document.

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

Chip Orange

thanks so much Jean-Guy for your quick reply.

What I was trying to do was to manipulate the variables collection through
the NormalTemplate object, without having to open the normal template first
as a document. Something like:

NormalTemplate.Variables("test").value = "y"

Except that the compiler tells me the template object has no variables
member.

All is ok, as you show, if I first open it as a document.

I guess that's just the way I need to do it.

thanks again.

Chip


Jean-Guy Marcil said:
Hi Chip,

Doc Variables can be in both templates and documents.

But, if you create them in a template, and then create a doc based on that
template, your "template doc variables" will also be in the document. I use
that all the time.
For example, sometimes I need to know if a document has been saved at least
once. The first time it is saved, I run a set of macros, and after that a
different set. For that, I create a doc variable in the template with a
value "x", then when the user saves the document, I check the doc variable
value, if it is equal to that of the value in the template - "x" in this
case - (i.e it has not been changed) then this is a first time save. I then
run the "first-time-save" macros and at the end change that doc variable
value to "y", for example. So, the next time the doc gets saved, the doc
variable is now equal to "y", which allows me to run a set of different
macros.
unless I open it as a document I'm unable to use them directly in the
template.
What are you trying to do exactly?

This
'_______________________________________
ScreenUpdating = False
With ActiveDocument.AttachedTemplate.OpenAsDocument
.Variables("UserName") = Application.UserName
.Close SaveChanges:=wdSaveChanges
End With
ScreenUpdating = True
'_______________________________________
will manipulate doc variables in the attached template, but will have no
impact in the active document.

Or this
'_______________________________________
Documents("MyDocument.doc").Variables("UserName") = Application.UserName
'_______________________________________
will manipulate variables in an open document.

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


"Chip Orange" <[email protected][remove-me-and-brackets]> a écrit
dans le message de news: (e-mail address removed)...
I'm trying to use document variables with the normaltemplate object, and I
thought I read that variables existed in both templates and documents; yet,
unless I open it as a document I'm unable to use them directly in the
template.

Did I just mis-interpret the documentation on document variables, are they
only available in documents?

thanks.

Chip
 

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