Fields in template

  • Thread starter Horatio J. Bilge, Jr.
  • Start date
H

Horatio J. Bilge, Jr.

I created a template of a form, and included SAVEDATE and LASTSAVEDBY to
indicate when I last updated the form. The problem I have is that when I
create a new document based on the template, the SAVEDATE and LASTSAVEDBY
fields are incorrect.

If I last updated the template on 10/1/2009, I want that date to stay on all
documents based on the template. And I want my name to stay for the
LASTSAVEDBY field. I considered just typing in the information, but I would
also like the fields to update when I make changes to the template.

Any suggestions?
Thanks,
~ Horatio
 
G

Graham Mayor

Using these fields in a template, when a new document is created from the
template that new document has never been saved, so the fields cannot
possibly show the information that you expect.

Frankly I cannot think of a simple way to do this that would work for you.
It may be possible to use an IncludeText field to insert a bookmarked entry
from the template, but I haven't time to test it now, as we are forecast a
power cut in ten minutes or so that will last at least all morning, while
the electricity authority make some major changes to the local power supply.
I'll look into it when the power returns.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

The power is back - but now that I have tested the premise, it isn't going
to work. I have however come up with another possibility.

Replace the SAVEDATE and LASTSAVEDBY fields in the template with docvariable
fields
{DocVariable varSavedDate} and {DocVariable varSavedBy} respectively. They
will not show any result in the template as the docvariables do not exist in
the template.

The macro opens the template and records the last saved details from the
template in docvariables in the new document then updates the fields.

If the template is re-saved, when the document is next opened the autoopen
macro causes the document to reflect the new data from the template Note
that without the condition in the autoopen macro, you will never be able to
open the template again for editing without Word crashing as it ties itself
in knots trying to resolve the process.

Sub AutoNew()
Call UpdateVariables
End Sub

Sub AutoOpen()
If ActiveDocument.FullName <> _
ActiveDocument.AttachedTemplate.FullName Then
Call UpdateVariables
End If
End Sub

Sub UpdateVariables()
Dim dSource As Document
Dim dTarget As Document
Dim oVars As Variables
Set dTarget = ActiveDocument
Set oVars = dTarget.Variables
Set dSource = Documents.Open(dTarget.AttachedTemplate.FullName)
oVars("varSavedDate").Value = _
dSource.BuiltInDocumentProperties(wdPropertyTimeLastSaved)
oVars("varSavedBy").Value = _
dSource.BuiltInDocumentProperties(wdPropertyLastAuthor)
For i = 1 To dTarget.Range.Fields.Count
If dTarget.Range.Fields(i).Type = wdFieldDocVariable Then
dTarget.Range.Fields(i).Update
End If
Next i
dSource.Close wdDoNotSaveChanges
End Sub

http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

I should have added that the macros should be saved in the document template
and not the normal template!

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Horatio J. Bilge, Jr.

Thanks for the help. That works great.
~ Horatio

Graham Mayor said:
The power is back - but now that I have tested the premise, it isn't going
to work. I have however come up with another possibility.

Replace the SAVEDATE and LASTSAVEDBY fields in the template with docvariable
fields
{DocVariable varSavedDate} and {DocVariable varSavedBy} respectively. They
will not show any result in the template as the docvariables do not exist in
the template.

The macro opens the template and records the last saved details from the
template in docvariables in the new document then updates the fields.

If the template is re-saved, when the document is next opened the autoopen
macro causes the document to reflect the new data from the template Note
that without the condition in the autoopen macro, you will never be able to
open the template again for editing without Word crashing as it ties itself
in knots trying to resolve the process.

Sub AutoNew()
Call UpdateVariables
End Sub

Sub AutoOpen()
If ActiveDocument.FullName <> _
ActiveDocument.AttachedTemplate.FullName Then
Call UpdateVariables
End If
End Sub

Sub UpdateVariables()
Dim dSource As Document
Dim dTarget As Document
Dim oVars As Variables
Set dTarget = ActiveDocument
Set oVars = dTarget.Variables
Set dSource = Documents.Open(dTarget.AttachedTemplate.FullName)
oVars("varSavedDate").Value = _
dSource.BuiltInDocumentProperties(wdPropertyTimeLastSaved)
oVars("varSavedBy").Value = _
dSource.BuiltInDocumentProperties(wdPropertyLastAuthor)
For i = 1 To dTarget.Range.Fields.Count
If dTarget.Range.Fields(i).Type = wdFieldDocVariable Then
dTarget.Range.Fields(i).Update
End If
Next i
dSource.Close wdDoNotSaveChanges
End Sub

http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>





.
 
P

Peter Jamieson

You could try putting this in your template:

{ IF "{ TEMPLATE \*Lower }" = "normal.dot*" "{ SET TLSB { LASTSAVEDBY }
}{ SET TSD { SAVEDATE } }

and use

{ TLSB } where you want the "template last saved by" value and
{ TSD } where you want the "template last saved date"

I haven't tested it much.

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv
 
G

Graham Mayor

Hmmm. That seemed a good (and refreshingly simple) plan, but unfortunately
it doesn't work, as the fields in the template are not updated to reflect
the last saved date/by properties until the template itself is next opened,
and so when a new document is created, the template is not opened and the
field shows the previously saved details.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Peter Jamieson

Good point. You can always save/open/save again and the template details
will then be OK, or at least as close as they probably need to be. Only
really for the VBA-averse, though.

I've obviously assumed that a template's template is /always/ normal
(.dot or .dotm), or that it can be made to be so, or that the field code
could be modified to use another known template name. You don't seem to
be able to change a template's template via the UI, but I imagine it
might be possible via some bit of code or direct modification of a .dotx
or .dotm.

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv
 
G

Graham Mayor

Peter said:
Good point. You can always save/open/save again and the template
details will then be OK, or at least as close as they probably need
to be. Only really for the VBA-averse, though.

Agreed, but would the users remember? To overcome the potential user memory
issues you would have to incorporate the double save in the template, which
would still require vba, which really takes us back where we started.
 
P

Peter Jamieson

Agreed, but would the users remember?

I agree with the general thrust of your argument. I think the VBA
approach is almost certainly the better one, particularly since the OP
sounds happy with it, but in the end it always comes down to which
trade-offs you want or have to make, and fortunately we can leave those
judgments to the person on the spot.

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv
 
H

Horatio J. Bilge, Jr.

I didn't expect my question to inspire such discussion. I don't mind using
VBA, so I went with Graham's suggestion. It worked great on my PC. But when I
put it on the Mac that I have to use at work, I thought it didn't work
(double-clicking on the template file just opened the template - not a new
document). When I "installed" the template by putting it in the templates
folder, it works fine.
 

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