PDA

View Full Version : Solved: Templates



Levski
05-16-2005, 07:28 AM
I would like a document to retain all the attributes it is assigned by the attached template so that once the file is moved away it will still independently have all the custom menu items, macros etc.

Help?!

Lev :help

TonyJollans
05-16-2005, 10:41 AM
Hi Levski,

It depends how much you have in your template - it may be quite hard. You will need some code in the Document_New event to copy whatever you want to copy.

BUT, what exactly do you mean by moved away from the template? A document ALWAYS has a relationship with a template. If the original one is not available Word will substitute Normal but what are the circumstances where the original one won't be available?

Levski
05-16-2005, 10:51 AM
If the .doc is opened on a computer which doesnt have the .dot, for examples sake. What I seek to achieve is the indepedence of the .doc so it contains the macros and menu/toolbar settings initially assigned by the template.

TonyJollans
05-16-2005, 11:06 AM
Hi Lev,

Two things ...

1. What do you want to copy? Code can be duplicated by manipulating the VBProject CodeModules (but it does need the "Trust Access to VB Project" security setting to be set). Toolbars can be copied using the OrganizerCopy method.

2. Why will the doc be available to a computer that doesn't have access to the dot? Are you e-mailing the document and don't want to send the template? There may be good reason for this, I just wonder :)

MOS MASTER
05-16-2005, 11:32 AM
Hi Lev, :yes

Tony has mentioned the most valuable information here but I have some new information for you to consider.

I totally agree with tony that you have to get anything in the document on which the document depends on the template for. (Toolbars/macros/Styles..etc...depends on how the templates build)

So on each of the newly created documents you have to copy a lot of stuff to that new document.

In relation to creating dynamic menu's and toolbars or whatever you're doing. The most important difference in Word in relation to let's say Excel is that you always have to set the Customation context (Where is the toolbar customation done) to the document/template that you want the toolbars to be in. This is extremly vital in Word!! (Otherwise you get lot's of different problems with toolbars be?ng saved to Normal.dot and other undesired results)

But like I said I don't know exactly which things you're doing exactly so let's leave it at that.

This is an interesting peace of line by tony:

1. What do you want to copy? Code can be duplicated by manipulating the VBProject CodeModules (but it does need the "Trust Access to VB Project" security setting to be set). Toolbars can be copied using the OrganizerCopy method.

Tony is right of course that you can program directly to the VBE to get all codemodules and what ever your document needs directly from the VBE.

But I like the second part of his tip better because he already explained you can use the OrganizerCopy method to copie your toolbar to your document. That's not only a fast method but also great advise...

The nice part in that method is that is also does copy Compleet CodeModules and this is of interest to you!

Why...simple,
If you set up your template this way:
* Have 1 Codemodule in the template with all your code.
* Have 1 Toolbar or perhaps more that all have there OnAction parameters to the macro's in that sub.

Then you can simply use something like this. (Simple to record with macrorecorder):

Sub Macro3()
Application.OrganizerCopy Source:= _
"C:\documents and settings\admin\desktop\Doc1.doc", Destination:= _
"Document2", Name:="Module1", Object:=wdOrganizerObjectProjectItems
Application.OrganizerCopy Source:= _
"C:\documents and settings\admin\desktop\Doc1.doc", Destination:= _
"Document2", Name:="gadgdg", Object:=wdOrganizerObjectCommandBars
End Sub


To get everything in there in a very fast way...

I would sugest you try it out.

Enjoy! :whistle:

TonyJollans
05-16-2005, 01:32 PM
Of course, Joost :) I wasn't thinking!

(Ook hebt je nog een heks - LOL)

MOS MASTER
05-17-2005, 10:02 AM
(Ook hebt je nog een heks - LOL)
WHAT!!! Where is she??? :devil:

Sorry Tony...I'll do my best but that women is haunting me! :rofl:

Howard Kaikow
05-19-2005, 02:30 AM
If the .doc is opened on a computer which doesnt have the .dot, for examples sake. What I seek to achieve is the indepedence of the .doc so it contains the macros and menu/toolbar settings initially assigned by the template.

templates serve a very useful purpose.

if a document is tied to a templates macros, then the proper thing is to distribute the template, as well as the document.

including unnecessary macros in a document is a nightmare if one needs to later change the macros. potentially, each document could carry with it the macros from the version of the template used to create the document, tbut then there may be several versions of those macros and you would have great difficulty updating the code.

fumei
05-20-2005, 11:42 AM
Howard is right, and version control IS a nightmare. The problem, of course, with distributing the .DOT file, along with a .DOC file, is file save location.

A .DOC file can be saved anywhere, it does not matter. But, a .DOT file must be, at least in order for Word to use it properly, in a specific location. Which means any distribution has to take care of that. However, the easiest way around that is to have on Document_Open, a snippet of code that checks the current folder for the matching .DOT file, and if found moves it to the correct location that the current version of Word looks for templates. That way you could send the .DOC and .DOT together and it would sort it out.

That being said, I am not quite solid on what the problem is.

MOS MASTER
05-20-2005, 11:58 AM
Hi Gerry, :yes

It's true that you should send your templates along with a document if it depends on it.

And it's good policy to send correct instructions where the addin should be. Like you said Word wil start searching for it's templates in the default location first.

But still it will find the template if it is in the same folder as the document which it should be connected to. (Don't know of the top of my head if this will work for each version though)

Tested with OS XP/ OF 2003:
Send a email with the document to another computer
Send a email with the template to another computer

Save them both in one folder. (I've used desktop)
Open the document. Result connection was made in a fast way and was permanet after moving the template again.

But I do agree some sort of installation code would be better because most users don't take the time to search for default file locations and stuff.

:whistle:

TonyJollans
05-20-2005, 01:38 PM
When Word opens a document it first looks for the template by name in the same directory as the document, and only if it doesn't find it there does it start to look elsewhere, including the path of the base template. This is certainly true in 2000 and later (can't confirm 97 at the moment but I believe it to be the same).

This makes it easy to distribute a document with a template and no need for code to do anything. Of course, recipients do need to know or be told to save the two to the same folder and then open the doc and not the dot. Unfortunately Word does not pick up a template zipped together with a document - they have to be unzipped to a folder first.

As I understand it documents do not have 'permanent' attachments to templates other than by name (even though a path is held with the document).

MOS MASTER
05-20-2005, 02:05 PM
Hi Tony, :yes

Ah that's a new one for me!
It was my understanding that Word is Searching in the default template folder first! (And then others like workgroup/startup)

If not there look in same folder. (Not found bind to Normal.dot)

So just curious is there documentation on this. I thought I had this out of books..but read so many that I haven't got a clue which one.

Yes permanent was ment in the way that ones it was connected it stays connected on the client it resides. So you could move it anywhere you like. It's binding is still there.

And I agree it's done by name only and if Word doesn't find it's Attacht template by name then it will bind to it's mother: Normal.dot (Cause Documents can't exist without a template it's bound to)

So what else do we have on this subject? :rofl:

TonyJollans
05-20-2005, 02:17 PM
I don't think I've seen any documentation on this - but that's the behaviour I have found through a process of trial and error over some time.

It's a bit hard to understand exactly how and why but (and I've got to be quick at the moment) ..

When you create a new document the full path and name of the template are stored with the document - you can see them in Tools > AddIns.
If you copy the template to the same folder as the document (and make some change so you know which template is being used) then open the document - it uses the version in the document folder and shows that path under Tools AddIns. If you change and save the document it does not save that path.
If you remove (or rename) all templates with the template name so that Word can't find one it 'attaches' Normal to the document for that session (see under References in the VBE Project Explorer) but Tools AddIns still shows the original path and name of the missing template so that next time the document is opened it will still look for the 'correct' template first.

MOS MASTER
05-20-2005, 02:26 PM
Hi Tony, :D

I've also looked at this before and I will look in to it tommorow to confirm if your theory is correct.

For me it doesn't matter which comes first because I now the outcome of the story..But If we've got this one clear it's a good reference for me for future questions on templates..(And there gonna be plenty of them) :rofl:

So I'll try your comments and do some testing..oh and look for online resources of MS...

Later. :whistle:

MOS MASTER
05-21-2005, 10:02 AM
Hi Tony, :yes

I took the test myself and you are right about The Samefolder being first! :bow:

What have I done:

Made a template and a document based on that template. The document is in folder SameFolderTest.

Added duplicate templates in 5 folders:

Templates folder
Workgroup templates folder
Word Startup folder
Desktop/MyDocuments (To check for special folders searching)
A folder with the name SameFolderTest (Where the document will be in)
In each of the duplicate templates I've added some text saying which template it is.

Then I run a macro to rename all duplicate templates. Now open the document. Like we mentioned before the old path remains in Tools/Addins but you can see in the VBE that it's attached template is now Normal.dot.

Closed the document and run a macro to rename all templates back to there original name. (So Word has a choise of 5 templates when he searches for the right one)

To mention the document was based on a template that resides in the Templates folder.

Now let's open the document again.
Go to Tools/Addins. And YEAH Tony's right the path in there points to the template in SameFolderTest!

Good news but we are suspecious so we need a second confirmation so I run this macro to check if it is indeed the same template:
Option Explicit
Sub ShowTemplate()
Dim oTemplate As Word.Document
Set oTemplate = ThisDocument.AttachedTemplate.OpenAsDocument
Set oTemplate = Nothing
End Sub


Great news the template we acspected to be the attached template was indeed the attached template! :friends:

Now I deleted the template in SameFolderTest to see where Word will look for his templates next.

I reopened and found that it was bound to the template in the Templates folder!

Did al my checks deleted it and went to the next.
I'll spear you all the details but this is the correct order which Word uses to look for it's templates:

A folder in which both the template (*.dot) and the document (*.doc) are. (SameFolder)
Word's default Template folder (Tools/File locations/Templates)
Word's Workgroup Templates folder (Tools/File Locations/Workgroup Templates)
None...
Regarding to the None:
So if the template isn't in any of the tree folders than Normal.dot will attach. (See in VBE)

As was mentioned before the next time you start the document the document will start his search from the beginning to find the original template it was attached to.

If you want to end that go to Tools/Addins and attach a new template and save the document.

It was a nice test do to Tony. (I'm happy knowing the right sequence now)

Thank you for the suggestion! :clap:

Howard Kaikow
05-21-2005, 11:14 AM
When Word opens a document it first looks for the template by name in the same directory as the document, and only if it doesn't find it there does it start to look elsewhere, including the path of the base template. This is certainly true in 2000 and later (can't confirm 97 at the moment but I believe it to be the same).

This makes it easy to distribute a document with a template and no need for code to do anything. Of course, recipients do need to know or be told to save the two to the same folder and then open the doc and not the dot. Unfortunately Word does not pick up a template zipped together with a document - they have to be unzipped to a folder first.

As I understand it documents do not have 'permanent' attachments to templates other than by name (even though a path is held with the document).

Ayup.

It has to be that way, lest there be even more havoc in distribting documents.

It is not uncommon for me to recive a document that has as its attaced template D:\Boo|Hoo\Whatever.dot".

What if someone receives such a document and does not have a drive D?
Word, correctly (ouch, ot hurt to admit that Word does something right!) uses a search algorithm to fond a likely candidate template.

Most of the time, this provides a successful solution and the user does not, and need not, know the difference.

However, users/programmers can get in trouble, e;g., by carelessly scattering vesions of Normal.dot in various directories, and when modifying a global template in a local directory.

I have the habit of keeping tghe source for my global templates all over the place, and a separate copy in te Startup directory.

This can cause havoc when changing the source copy if the global copy is in Startup.

MOS MASTER
05-21-2005, 12:23 PM
Ayup.

It has to be that way, lest there be even more havoc in distribting documents.

Hi Howard,

I've tested this and yes this is the outcome! (Thank God I may add)

And if it upfront had to be??? Don't think so.
It would be more logical for me if the Template folder was the first template Word would look in.
Why? Simple if you save a document as a template you get the default template folder in the SaveDialog..Hence why I'd suspect that folder to be the default. (But after testing I now know it isn't)

So yes I'm glad MS has provided this search order for easy distribution!

But I now you love documentation...So have you come about anything on this perticular subject? (the one my test is about)



I have the habit of keeping tghe source for my global templates all over the place, and a separate copy in te Startup directory.

This can cause havoc when changing the source copy if the global copy is in Startup.
Yes I'm kinda like that to. I wish my archiving abillaties where as good at home as they are at work!

This has brought me many frustrated moments in the past when I didn't clean up a perticular template and had several templates loaded together..

O well where's the fun if there aren't any problems left to conquer! :rofl:

Howard Kaikow
05-21-2005, 02:32 PM
the point was to make folkes aware that there is a search sequence and what is the search sequence.

and, i have seen the sequence described in msft kb or book.

and, it is correct to first look in the directory in which the doc file lives, shocking as it may be, msft did this right (ouch, there goes that sharp pain again!).

MOS MASTER
05-21-2005, 02:38 PM
Hi Howard, :yes

Yes that was the point and it was interesting enough to investigate what the exact sequence was.

For me this topic will be a good reference when I'm in that stage again in which I've totaly forgotten about the sequence and I might actually need it! :rofl:

Yes it's truly amazing..but Bill's crew did do something right here. :devil:

I also remembered to have read it in one of my books and because I've read many I can't remember which!

Oh well maybe I'll stumble over the artical in MSFT KB later...:whistle:

Howard Kaikow
05-21-2005, 02:54 PM
Oh well maybe I'll stumble over the artical in MSFT KB later...:whistle:

Many of the good articles I've seen are found when I'm looking for something else.

MOS MASTER
05-21-2005, 02:57 PM
Many of the good articles I've seen are found when I'm looking for something else.
Yepz..It's like looking for sugar to put in your coffee and you just can't find it.
(Guess what..it's been standing there in front of you the whole time!!!) :think:

TonyJollans
05-22-2005, 10:04 AM
In the interests of completeness, the order is:

1. Same directory as document
2. User templates directory
3. Workgroup templates directory
4. Path of attached template (if different from all above)
5. Give up and look for Normal

A note about number 4. This can cause problems (very slow opening) if it's a network path which is not available to the person opening the document.

A final note amid all the congratulation of Microsoft. I find it rather odd that it doesn't look at already open templates anywhere in its search chain. It is perfectly possible for the 'correct' template to be open and for Word to use Normal.

MOS MASTER
05-22-2005, 03:14 PM
In the interests of completeness, the order is:

1. Same directory as document
2. User templates directory
3. Workgroup templates directory
4. Path of attached template (if different from all above)
5. Give up and look for Normal
Hi Tony, :yes

You're absolutly right of course! I totaly forget about that possibility in my test!

That was quit stupid :banghead: of me because I've written several subs (To batch rename to new location (Attached template)) exactly for that problem!! (Renaming the server is the most common one I know of)

And yes it can take a very long time for templates to open if they are attached somewhere on the network on a non default location...



A final note amid all the congratulation of Microsoft. I find it rather odd that it doesn't look at already open templates anywhere in its search chain. It is perfectly possible for the 'correct' template to be open and for Word to use Normal.
I agree that's no good!

Logics for me would still be that Word would search for the template that it's attached to first. (Nr 4 in the list)

Because our little test has proven that if you keep templates lying arround in your network you can easily screw up things if you work with different versions...(Because Word will keep this order)

I can see major problems with this and I'm glad we've taken the time to get to the bottom of this..(And perhaps there's even more that we haven't seen yet) :rofl:

It was a pleasure as always! :hi: