MS Office Forum / Word / Programming / July 2007
Template Assignment Problem
|
|
Thread rating:  |
George Lee - 02 Jul 2007 15:54 GMT I am trying to programmatically change the assigned template:
sourceDocument.AttachedTemplate = "\\CorporateServer1\SubDirectory\CorporateTemplate.dot"
The problem is sometimes the template gets changed and sometimes it doesn’t, with a pattern I can’t figure out. Is there a reason a template would not be changed or a bug to workaround?
Klaus Linke - 02 Jul 2007 19:26 GMT >I am trying to programmatically change the assigned template: > [quoted text clipped - 5 lines] > be > changed or a bug to workaround? Hi George,
Not sure... Sometimes it takes a long time to locate the template on the server, so maybe it's some timeout problem.
You might try to debug the problem with something like this:
Dim Start Start = Timer Dim sTemplate As String sTemplate = "\\CorporateServer1\SubDirectory\CorporateTemplate.dot"
' Test if template is found: If Dir(sTemplate) = "" Then MsgBox sTemplate & " not found!", vbCritical Exit Sub End If
' Try more than once if necessary, up to one second: Do sourceDocument.AttachedTemplate = sTemplate If Timer - Start > 1 Then MsgBox "Current attached template: " & _ sourceDocument.AttachedTemplate.FullName, _ vbCritical, "Template not found?" Exit Sub End If Loop Until (sourceDocument.AttachedTemplate = MID(sTemplate, InStrRev(sTemplate, "\") + 1))
Regards, Klaus
George Lee - 03 Jul 2007 14:44 GMT Thanks. It never occurred to me as a timing problem. I did incorporate this into the code.
In addition, just observing the problem, it looks also like a naming thing such as if the new template has the same name as the one being replaced. For example, I made a copy of the template and put it on the network location. However, if I change the attached template location (either programmatically or through the Word dialog), it resets to the owning folder path name.
> >I am trying to programmatically change the assigned template: > > [quoted text clipped - 39 lines] > Regards, > Klaus Klaus Linke - 03 Jul 2007 16:00 GMT > Thanks. It never occurred to me as a timing problem. I did incorporate > this into the code. [quoted text clipped - 6 lines] > programmatically > or through the Word dialog), it resets to the owning folder path name. Not sure I'm following you, and I'm an idiot about network/Windows issues anyway. Maybe it's an issue with mapped drives? What you see as a local drive might really be on the server (or vice versa) ...
Klaus
Shauna Kelly - 03 Jul 2007 17:09 GMT Hi George
I'm not sure I'm following you either. But I wonder if you have a file with the same name as your template on your local machine. Word stores, but ignores, the location of a template. Let's say you have a document that was based on \\myserver\\myshare\\x.dot. And you have a template named x.dot in your local UserTemplates file. When you open the document, Word looks through the list of places it likes to look for templates. And it finds x.dot in your User Templates folder. That's the one it will now regard as attached to the document, and ActiveDocument.AttachedTemplate.FullName will now report that the document is attached to the template in your User Templates folder.
Hope this helps.
Shauna Kelly. Microsoft MVP. http://www.shaunakelly.com/word
>> Thanks. It never occurred to me as a timing problem. I did incorporate >> this into the code. [quoted text clipped - 14 lines] > > Klaus George Lee - 03 Jul 2007 17:18 GMT That's exactly it. If I ever later attach a template of the same name, it will always use the original location. How is that not a bug? Is there a workaround?
> Hi George > [quoted text clipped - 32 lines] > > > > Klaus Shauna Kelly - 04 Jul 2007 04:11 GMT Hi George
When Word opens a document it doesn't use the template in the original location. It uses the first template it finds with the right name. And it looks for that template in specific places in a specific order. Just now I can't find any current documentation from Microsoft on what that order is. And in any case, it depends on the version of Word (this is the info for Word 2000: http://support.microsoft.com/kb/220502.)
The workaround is simple: don't have more than one template with the same name in any of the following places: User Templates folder, Workgroup templates folder, the folder that contains the document or the folder that holds the template that you want to attach to the document.
And for what it's worth, I agree with Jonathan. This might be useful: http://support.microsoft.com/kb/830561/
Hope this helps.
Shauna Kelly. Microsoft MVP. http://www.shaunakelly.com/word
> That's exactly it. If I ever later attach a template of the same name, it > will always use the original location. How is that not a bug? Is there a [quoted text clipped - 42 lines] >> > >> > Klaus Jonathan West - 03 Jul 2007 18:31 GMT >I am trying to programmatically change the assigned template: > [quoted text clipped - 5 lines] > be > changed or a bug to workaround? I would strongly recommend you never set the attached template to a network location. Always store templates locally.
If the server becomes unavailable, there is a bug in Word that means it can take several minutes to open a document based on a template located on the server. There are other good reasons not to put templates on the network. e.g. limiting network traffic, improving response times in Word, ease of updating templates.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
George Lee - 05 Jul 2007 15:02 GMT I understand that. Even so, is there a workaround for the template naming problem?
> >I am trying to programmatically change the assigned template: > > [quoted text clipped - 14 lines] > e.g. limiting network traffic, improving response times in Word, ease of > updating templates. Russ - 06 Jul 2007 03:08 GMT Questions for the group: Is it really possible to *share* a template .dot file on a network. I know Windows can share .dll files among many programs simultaneously. When you attach a template, is it the same as Windows opening a file and locking out other users, which would mean only one user at a time? So maybe Word just opens the file long enough to read and apply the template data when attached and then tries to release the .dot before someone else needs it? Do you have to make the .dot on the server a 'read only' file?
> I understand that. Even so, is there a workaround for the template naming > problem? [quoted text clipped - 17 lines] >> e.g. limiting network traffic, improving response times in Word, ease of >> updating templates.
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Shauna Kelly - 06 Jul 2007 07:46 GMT Hi Russ
Yes, it is possible for users to share a template .dot file on a network. This is possible because of the two ways that a template interacts with a Word document. When a Word document is created, it inherits certain things from its parent template. When a Word document is open, the template to which it is attached makes certain things available to the document (note: "makes available to", not "stores in"). See the following for more info: What is the relationship between a Word document and its template? http://www.ShaunaKelly.com/word/templaterelations/index.html
In the normal course of events, if you have a template on the local machine, you might have open several documents that are attached to that one template. Similarly, several users can each have open several documents that are all attached to one template on a server.
If you want to prevent users from making changes to a template stored on a server, then you should mark it as read only.
However, there are several disadvantages to setting up an organization so that users share a template: (a) if the network goes down, no-one can do any work (b) if a user unplugs the laptop and takes it home, or to the local coffee shop, or to a meeting room, or on to a plane, then they can't do any work (c) if the name of the network share changes, Word will take ages to open the document (see http://support.microsoft.com/kb/830561/) (d) updating the template can be difficult.
The best solution is a logon script to copy new or updated templates to the local machine.
Hope this helps.
Shauna Kelly. Microsoft MVP. http://www.shaunakelly.com/word
> Questions for the group: > Is it really possible to *share* a template .dot file on a network. I know [quoted text clipped - 35 lines] >>> e.g. limiting network traffic, improving response times in Word, ease of >>> updating templates. Russ - 06 Jul 2007 08:15 GMT Shauna, Yes, I think I was fishing for answers for why it was bad to actively *use* a template on the server. Thanks for the information. I agree; using a script to download the new master template version to the local machine is best.
> Hi Russ > [quoted text clipped - 71 lines] >>>> e.g. limiting network traffic, improving response times in Word, ease of >>>> updating templates.
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Jonathan West - 06 Jul 2007 10:39 GMT > Hi Russ > [quoted text clipped - 23 lines] > the document (see http://support.microsoft.com/kb/830561/) > (d) updating the template can be difficult. A few more reasons...
e. I've known VBA code in templates to work differently when the template was open from two different users compared to how it works when there is just one user holding it open. The problem is not predictable nor common, but when it does occur, it is reproducible.
f. network templates are generally slower to open than locally-stored ones.
g. You are loading the network with unnecessary traffic which reduces the bandwidth available for other applications that really must use the network.
h. If the template gets corrupted (it can happen from time to time), it is corrupted for all users. At least with a locally stored template only one user is affected until you get a good copy back onto that PC.
 Signature Regards Jonathan West - Word MVP www.intelligentdocuments.co.uk Please reply to the newsgroup
George Lee - 09 Jul 2007 14:44 GMT This is a great discussion and I appreciate the help. However, while I understand most of this ideas, the fact is, it's not my choice where the template is or how people access it.
My immediate issue is how to assign a template in a different location but having the same name? Is there a reg entry that needs to be deleted?
> > Hi Russ > > [quoted text clipped - 39 lines] > corrupted for all users. At least with a locally stored template only one > user is affected until you get a good copy back onto that PC. Russ - 10 Jul 2007 00:39 GMT >Quote In addition, just observing the problem, it looks also like a naming thing such as if the new template has the same name as the one being replaced. For example, I made a copy of the template and put it on the network location. However, if I change the attached template location (either programmatically or through the Word dialog), it resets to the owning folder path name.
>Unquote Are you saying that in your Word, not someone else's, you save a copy to the network. Later in your Word, in a new document, you try to attach the network copy, but your Word sees your local copy first? Maybe you need to restart your Word. Or I would just move your original developing template out of your Word's normal paths for Templates. You can change those paths manually or through code for your Word. Everybody else who is on the network and who doesn't have a copy stored locally probably doesn't have a problem opening that network copy, other than the reasons given earlier in this thread. The other people should have a script that downloads the new version from the network onto their machine, which has the template paths are set up to look locally and not on the network.
> This is a great discussion and I appreciate the help. However, while I > understand most of this ideas, the fact is, it's not my choice where the [quoted text clipped - 46 lines] >> corrupted for all users. At least with a locally stored template only one >> user is affected until you get a good copy back onto that PC.
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
George Lee - 10 Jul 2007 21:50 GMT Try it yourself. Create two templates named the same but in different folders. Attach one template and save the document. Now, try attaching the other template. It should revert to the first saved one. It doesn't seem to matter where the files are, or even if its on a network location.
> >Quote > In addition, just observing the problem, it looks also like a naming thing [quoted text clipped - 66 lines] > >> corrupted for all users. At least with a locally stored template only one > >> user is affected until you get a good copy back onto that PC. Russ - 10 Jul 2007 22:07 GMT Ah, Now I see what you a saying. A quick reply, I haven't tested. Maybe if you record a macro to see how to use the organizer To uncheckbox a document's currently opened attached template and then attach the other template. You didn't want both attached at the same time, did you?
> Try it yourself. Create two templates named the same but in different > folders. Attach one template and save the document. Now, try attaching the [quoted text clipped - 72 lines] >>>> corrupted for all users. At least with a locally stored template only one >>>> user is affected until you get a good copy back onto that PC.
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
rhana@motorola.com - 13 Jul 2007 17:08 GMT On Jul 6, 2:46 am, "Shauna Kelly" <ShaunaKe...@SendNoSpamToShaunaKelly.com> wrote: [snip]
> Yes, it is possible for users to share a template.dot file on a network. [snip]
> In the normal course of events, if you have a template on the local machine, > you might have open several documents that are attached to that one template. [quoted text clipped - 6 lines] > However, there are several disadvantages to setting up an organization so > that users share a template: [snip]
> The best solution is a logon script to copy new or updated templates to the > local machine. Hi, Shauna,
I've created several Word templates that currently reside on our departmental server. I need to keep them there for ease of maintenance and compliance reasons. However, I'd like to explore your suggestion to create a script to copy the templates to my users' local machines. Can this be done in Word VBA; for example, when the user launches Word (Normal.dot) or opens a document based on one of the templates (myTemplate.dots), the template is automatically copied to the local machine? Or must this be done on the Windows server side, as part of our network's Windows logon script?
Thanks in advance for any suggestions you (or anyone else here) can offer. My apologies if I should be asking this in a different forum. (Windows Server admin?)
Rhana Cassidy Production Editor, N&NM Technical Publications Motorola
P.S. I've found your posts and articles incredibly helpful as I've been writing macros for my templates. Thanks!
Shauna Kelly - 15 Jul 2007 03:08 GMT Hi Rhana
Messing with users' normal.dot files is not generally a good idea. Normal.dot should be the individual user's preserve.
However, your fundamental idea (download the files when the user first starts Word) is possible. Put the relevant code in a .dot file and put the .dot file in the user's Word Startup folder (the folder identified at Tools
> Options > File Locations > Startup). There is info here on how to get code to run automatically when Word starts: http://word.mvps.org/faqs/MacrosVBA/PseudoAutoMacros.htm
It is more usual, however, to get the IT guys to write a script that runs when the user logs on to the network.
There are several good reasons for this. Here are two examples: (a) Consider the user who brings the laptop into the office and connects to the network. Does some work, but does not open Word. Disconnects the laptop from the and takes it to the coffee shop. If you had waited until the user opened Word to copy down the templates, then this user has not received the latest templates. If you do it at logon, the user can't avoid getting the updated templates.
(b) A user can always stop Word running macros. So the user can avoid getting the new templates if you rely on code running within Word.
Hope this helps.
Shauna Kelly. Microsoft MVP. http://www.shaunakelly.com/word
> On Jul 6, 2:46 am, "Shauna Kelly" > <ShaunaKe...@SendNoSpamToShaunaKelly.com> wrote: [quoted text clipped - 49 lines] > been writing > macros for my templates. Thanks! RhanaJoy - 16 Jul 2007 18:37 GMT On Jul 14, 10:08 pm, "Shauna Kelly" <ShaunaKe...@SendNoSpamToShaunaKelly.com> wrote:
> Hi Rhana > [quoted text clipped - 24 lines] > > Shauna Kelly. Microsoft MVP.http://www.shaunakelly.com/word Hi, Shauna,
Yes, this does help. I wasn't quite sure of the difference between downloading templates on Word startup and on login. Since our templates don't change very often, I think I'll take the easy way out and just e-mail them to the staff when needed. It seems so low-tech, though. :)
Thanks again.
Rhana Cassidy
|
|
|