Hi gang,
I have users taking turns working on a document based on the same template. These users are working on individual computers. They each have a copy of the template installed in the template folder. The template folder path is different for each individual.
I have managed to write code that points the document to the default user template folder and attaches the right template to this document. I am half way there.
I need to create an AutoOpen in every document that will be created based on this template so that every time a user opens the document it will attach the template from that user’s default template folder. I know I will have to put this code in the AutoNew in the template but that is where my “expertise” ends.
Please help.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Creating code in a .doc (Word 2000-97 PC-MAC)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Creating code in a .doc (Word 2000-97 PC-MAC)
- This topic has 14 replies, 5 voices, and was last updated 20 years, 5 months ago.
AuthorTopicWSJCSolutions
AskWoody LoungerDecember 7, 2004 at 2:24 am #413227Viewing 1 reply threadAuthorReplies-
Andrew Lockton
AskWoody_MVPDecember 7, 2004 at 3:10 am #909909Are you sure you really need this? Is there other things your macro needs to do beyond just finding a template to attach?
When Word opens a file it has a cascading process for finding the template that is attached to the file. The order appears to be something like this…
1. Use the template in the location specified by the full path for the Attached Template
2. If the template is not there then look in User Templates location for a template with the same name
3. If the template is not there then look in the Workgroup Templates location for a template with the same name
4. If the template is not there then look in the same folder that the Word file is in.5. Give up looking for the template – the Word file will open but you don’t have access to the attached template.
My version of the order is likely to be wrong but the important thing is that Word will happily find the template if it is located in one of four places.
-
WSJCSolutions
AskWoody LoungerDecember 7, 2004 at 1:20 pm #910043Hi Andrew,
Tks for the tip. The workgroup template seems like the logical approach. However, there are dozens of templates and they are separated in different sub folders in the user and/or workgroup templates location. If I put all the templates in a de fault folder it will make it more difficult for the user to find the right template when creating a new doc. By adding code to every new file created with a specific template I can specify the sub-folder in which this template is located.
Would there be a better way of doing this?
Tks -
Andrew Lockton
AskWoody_MVPDecember 7, 2004 at 10:35 pm #910332JC
I would be surprised if you need so many templates that folders are really necessary. You can minimise the number of templates you have by following a philosophy of grouping related content inside a common template.
I prefer to create templates so that some of them contain multiple content options. One template can have different contents added automatically at the start depending on what the user decides they want in the file. This way I can create a consistent look across a series of files and only have to maintain styles/logos/code in one template. The different content options are stored as autotext entries in the template and an autonew macro displays the list which is built dynamically based on the autotext entries in the template at the time.
An example is the difference between having 20 different templates for online forms to having one forms template which allows the user to choose which one of the 20 forms the file should contain.
This approach makes it easy to manage updates to the template and automatically caters for new or changed forms at any time. In some templates the autotext entries might contain 100s of pages of content and the parts that are common to all files (eg cover, disclaimer, toc) are only stored once.
If you are wedded to the separate templates model, what about using a macro in the template that looks to a custom property in the file that contains the path to where the template is held. The macro could then run every time the file is opened and work out which path to set the attached template to by using that path contained in the file. This would let you avoid putting code specifically in the document but still enable a macro to run that is specific to that file.
-
Andrew Lockton
AskWoody_MVPDecember 7, 2004 at 10:35 pm #910333JC
I would be surprised if you need so many templates that folders are really necessary. You can minimise the number of templates you have by following a philosophy of grouping related content inside a common template.
I prefer to create templates so that some of them contain multiple content options. One template can have different contents added automatically at the start depending on what the user decides they want in the file. This way I can create a consistent look across a series of files and only have to maintain styles/logos/code in one template. The different content options are stored as autotext entries in the template and an autonew macro displays the list which is built dynamically based on the autotext entries in the template at the time.
An example is the difference between having 20 different templates for online forms to having one forms template which allows the user to choose which one of the 20 forms the file should contain.
This approach makes it easy to manage updates to the template and automatically caters for new or changed forms at any time. In some templates the autotext entries might contain 100s of pages of content and the parts that are common to all files (eg cover, disclaimer, toc) are only stored once.
If you are wedded to the separate templates model, what about using a macro in the template that looks to a custom property in the file that contains the path to where the template is held. The macro could then run every time the file is opened and work out which path to set the attached template to by using that path contained in the file. This would let you avoid putting code specifically in the document but still enable a macro to run that is specific to that file.
-
WSJanB
AskWoody LoungerDecember 8, 2004 at 11:21 am #910472I fully agree with Andrew concerning the use of multi-purpose templates.
When I took over from my predecessor, there were 300+ templates, scattered all over the place (company server, department server, individual PC’s). Now there are about 70 multi-purpose templates left, all sitting in the WorkgroupTemplates folder.
About 45 are general templates in the main folder, visible to all users. The others are sitting in 4 subfolders named Dep 1, …, Dep 4. Those subfolders are only accessible by authorised users (Novell network folders authorisation model) and so they see a tab with the name of the appropriate department(s), listing the dedicated templates for that department.
Our users are very pleased with this, as it’s very much easier for them to find the template they need and they get “guidance” in the templates themselves as to which choices to make. -
WSJanB
AskWoody LoungerDecember 8, 2004 at 11:21 am #910473I fully agree with Andrew concerning the use of multi-purpose templates.
When I took over from my predecessor, there were 300+ templates, scattered all over the place (company server, department server, individual PC’s). Now there are about 70 multi-purpose templates left, all sitting in the WorkgroupTemplates folder.
About 45 are general templates in the main folder, visible to all users. The others are sitting in 4 subfolders named Dep 1, …, Dep 4. Those subfolders are only accessible by authorised users (Novell network folders authorisation model) and so they see a tab with the name of the appropriate department(s), listing the dedicated templates for that department.
Our users are very pleased with this, as it’s very much easier for them to find the template they need and they get “guidance” in the templates themselves as to which choices to make. -
WSGary Frieder
AskWoody LoungerJanuary 1, 2005 at 8:40 am #917383Ms. JC,
Just stumbled on this thread, and don’t know if it’s now outdated and you have found a solution, anyway:
Notwithstanding Andrew and Jan’s valid suggestions about reducing the number of templates you (and your users) have to deal with, it seems like there is still a missing link in the situation you are describing – are you saying you need this code to run, every time a document based on a specific template is opened, to locate the appropriate template on that individual user’s profile, and reattach that template?
If so, then housing the needed code in the template’s Document_Open or AutoOpen procedure won’t do the trick, because that code won’t even run when the document is opened, if the document is not currently attached to the relevant template that contains the code.
Neither is putting the code into a procedure in the document itself a good approach – as a general rule, you don’t want to be propagating code-bearing documents out into the world – code should stay in the template.
If you do have to run some code to manage the locations of lots of different templates and attach documents to correct templates on Open, then the best place to house that code would be in a global add-in template. You would need to set up an application-level DocumentOpen event procedure. Code in that procedure could determine the name of the template for every document that opens, and then further code could attach the correct template, for those documents that need it.
If the above is relevant to what you’re trying to do, and you need more detail, write back and I’ll track down some working code.Gary
-
WSfrankpasztor
AskWoody Lounger -
WSGary Frieder
AskWoody LoungerJanuary 4, 2005 at 1:43 am #918053Assuming for the moment that we’re going to house the code in the document itself – in its ThisDocument module – the next question would be: how do you get the code in there to begin with?
If you put code in a template’s ThisDocument module, that code isn’t going to propagate out to the ThisDocument module of the documents that get created based on the template. So, you’d either have to:
(1) abandon creating documents based on templates. Instead, put all of the needed code into a ‘document which serves as a template’, and always create new documents by opening this ur-document and doing a FileSaveAs. That does create some circular logic though, because if the documents are not going to be based on a template, then you don’t need to be running code to find the document’s template, anyway! The main problem with this approach is that you’re working against the way Word is designed, and giving up the advantages of housing things like code, custom toolbars and autotexts in the template, rather than the document. (On the other hand, that’s how it works in Excel….)
(2) stay with the template/document structure, and have code in the template’s Document_New procedure, which writes the needed code into the Document_Open procedure of each new document, at the time that it’s created – maybe that’s what the original poster is looking to do. Haven’t ever done that myself, but I’m sure it’s possible to do – I’d be nervous that this approach might set off alerts from antivirus software, but if you can get past that, it might be OK.
I still think it’s more straightforward to have any needed code like this, live in a global template, rather than either of (1) or (2) above.
Gary
-
WSGary Frieder
AskWoody LoungerJanuary 4, 2005 at 1:43 am #918054Assuming for the moment that we’re going to house the code in the document itself – in its ThisDocument module – the next question would be: how do you get the code in there to begin with?
If you put code in a template’s ThisDocument module, that code isn’t going to propagate out to the ThisDocument module of the documents that get created based on the template. So, you’d either have to:
(1) abandon creating documents based on templates. Instead, put all of the needed code into a ‘document which serves as a template’, and always create new documents by opening this ur-document and doing a FileSaveAs. That does create some circular logic though, because if the documents are not going to be based on a template, then you don’t need to be running code to find the document’s template, anyway! The main problem with this approach is that you’re working against the way Word is designed, and giving up the advantages of housing things like code, custom toolbars and autotexts in the template, rather than the document. (On the other hand, that’s how it works in Excel….)
(2) stay with the template/document structure, and have code in the template’s Document_New procedure, which writes the needed code into the Document_Open procedure of each new document, at the time that it’s created – maybe that’s what the original poster is looking to do. Haven’t ever done that myself, but I’m sure it’s possible to do – I’d be nervous that this approach might set off alerts from antivirus software, but if you can get past that, it might be OK.
I still think it’s more straightforward to have any needed code like this, live in a global template, rather than either of (1) or (2) above.
Gary
-
-
WSfrankpasztor
AskWoody Lounger
-
-
WSGary Frieder
AskWoody LoungerJanuary 1, 2005 at 8:40 am #917384Ms. JC,
Just stumbled on this thread, and don’t know if it’s now outdated and you have found a solution, anyway:
Notwithstanding Andrew and Jan’s valid suggestions about reducing the number of templates you (and your users) have to deal with, it seems like there is still a missing link in the situation you are describing – are you saying you need this code to run, every time a document based on a specific template is opened, to locate the appropriate template on that individual user’s profile, and reattach that template?
If so, then housing the needed code in the template’s Document_Open or AutoOpen procedure won’t do the trick, because that code won’t even run when the document is opened, if the document is not currently attached to the relevant template that contains the code.
Neither is putting the code into a procedure in the document itself a good approach – as a general rule, you don’t want to be propagating code-bearing documents out into the world – code should stay in the template.
If you do have to run some code to manage the locations of lots of different templates and attach documents to correct templates on Open, then the best place to house that code would be in a global add-in template. You would need to set up an application-level DocumentOpen event procedure. Code in that procedure could determine the name of the template for every document that opens, and then further code could attach the correct template, for those documents that need it.
If the above is relevant to what you’re trying to do, and you need more detail, write back and I’ll track down some working code.Gary
-
-
WSJCSolutions
AskWoody LoungerDecember 7, 2004 at 1:20 pm #910044Hi Andrew,
Tks for the tip. The workgroup template seems like the logical approach. However, there are dozens of templates and they are separated in different sub folders in the user and/or workgroup templates location. If I put all the templates in a de fault folder it will make it more difficult for the user to find the right template when creating a new doc. By adding code to every new file created with a specific template I can specify the sub-folder in which this template is located.
Would there be a better way of doing this?
Tks
-
-
Andrew Lockton
AskWoody_MVPDecember 7, 2004 at 3:10 am #909910Are you sure you really need this? Is there other things your macro needs to do beyond just finding a template to attach?
When Word opens a file it has a cascading process for finding the template that is attached to the file. The order appears to be something like this…
1. Use the template in the location specified by the full path for the Attached Template
2. If the template is not there then look in User Templates location for a template with the same name
3. If the template is not there then look in the Workgroup Templates location for a template with the same name
4. If the template is not there then look in the same folder that the Word file is in.5. Give up looking for the template – the Word file will open but you don’t have access to the attached template.
My version of the order is likely to be wrong but the important thing is that Word will happily find the template if it is located in one of four places.
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Phishers extract Millions from HMRC accounts..
by
Microfix
4 hours, 57 minutes ago -
Windows 10 22H2 Update today (5 June) says up-to-date but last was 2025-04
by
Alan_uk
6 hours, 49 minutes ago -
Thoughts on Malwarebytes Scam Guard for Mobile?
by
opti1
9 hours, 57 minutes ago -
Mystical Desktop
by
CWBillow
10 hours, 11 minutes ago -
Meta and Yandex secretly tracked billions of Android users
by
Alex5723
4 hours, 57 minutes ago -
MS-DEFCON 2: Do you need that update?
by
Susan Bradley
22 minutes ago -
CD/DVD drive is no longer recognized
by
WSCape Sand
1 day, 1 hour ago -
Windows 11 24H2 Default Apps stuck on Edge and Adobe Photoshop
by
MikeBravo
1 day, 4 hours ago -
North Face and Cartier customer data stolen in cyber attacks
by
Alex5723
1 day, 2 hours ago -
What is wrong with simple approach?
by
WSSpoke36
18 hours, 51 minutes ago -
Microsoft-Backed Builder.ai Set for Bankruptcy After Cash Seized
by
Alex5723
1 day, 13 hours ago -
Location, location, location
by
Susan Bradley
4 hours, 10 minutes ago -
Cannot get a task to run a restore point
by
CWBillow
1 day, 15 hours ago -
Frustrating search behavior with Outlook
by
MrJimPhelps
1 day, 5 hours ago -
June 2025 Office non-Security Updates
by
PKCano
2 days, 1 hour ago -
Secure Boot Update Fails after KB5058405 Installed
by
SteveIT
4 hours, 30 minutes ago -
Firefox Red Panda Fun Stuff
by
Lars220
2 days, 1 hour ago -
How start headers and page numbers on page 3?
by
Davidhs
2 days, 12 hours ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
1 day, 14 hours ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
2 days, 20 hours ago -
Windows 11 Insider Preview build 26120.4230 (24H2) released to BETA
by
joep517
2 days, 20 hours ago -
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
2 days, 10 hours ago -
Firefox 139
by
Charlie
2 days, 3 hours ago -
Who knows what?
by
Will Fastie
1 day, 5 hours ago -
My top ten underappreciated features in Office
by
Peter Deegan
2 days, 21 hours ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
15 hours, 16 minutes ago -
Misbehaving devices
by
Susan Bradley
1 day, 17 hours ago -
.NET 8.0 Desktop Runtime (v8.0.16) – Windows x86 Installer
by
WSmeyerbos
4 days, 3 hours ago -
Neowin poll : What do you plan to do on Windows 10 EOS
by
Alex5723
1 day, 2 hours ago -
May 31, 2025—KB5062170 (OS Builds 22621.5415 and 22631.5415 Out-of-band
by
Alex5723
4 days, 2 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.