none
How to stop docs created from templates from keeping that Template Path RRS feed

  • Question

  • Hi, our software allows clients to create documents from templates we provide.

    The issue we're having is if they move their server, the old documents will attempt to contact that template network path that doesn't exist any more and can take a very long time to open. I have ways to clear the template path from existing documents (and a registry key can be created to set that timeout time very low) but I want to stop this from happening in the future.

    We don't care about propagating any changes from the templates to existing documents, so does anyone know how to stop Word from attaching the template path to resultant documents (so docs created from a template will just have Normal as their AttachedTemplate)?

    Thank you

    Tuesday, November 27, 2012 7:26 PM

Answers

  • Anyway, when you write "You can change which template is used, but you must have one", please explain why it ist possible to delete the reference with no impact on the content and format of this document.

    All that does is to change the template to 'Normal', so you still have one.

    Granted, access to a particular template may not be necessary for mere document viewing, but it does matter when one is trying to manage document editing, especially if you're trying to ensure Style consistency via the 'Automatically update document Styles' setting. As you note, macro dependencies are another area where it matters.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, November 29, 2012 1:28 AM
  • Hi loneil

    <<1)The program creates a new document based on the selected template.

    2)The macros are inserted into the document and run to populate data

    3)The macros are removed and the document is saved as a docx>>

    If macros are allowed at all, then why not put the necessary macros in a dotm template that's not attached to the document, but loaded as an Add-in (Application.Addins). Your program could load and unload it as required. And that same global template could also take care of changing the template attachment.

    This template could be distributed as part of your solution, which is probably less "tricky" to manage as an update than needing to change templates installed for use by the user on their machines.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 29, 2012 6:42 AM
    Moderator

All replies

  • You could add a 'Document_New' macro to the template that sets the new document's attached template to nothing (ie ActiveDocument.AttachedTemplate = ""). That will change the attached template to 'Normal'. Do note that this can break the functionality of documents that are meant to run macros found in their original templates. It can also have adverse effects where the 'Automatically update document styles' setting is checked.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, November 27, 2012 11:35 PM
  • Thanks for the response, yeah we're fine with the resultant documents being created from the templates having no link to their templates (and all the loss of functionality, which we don't use, that comes with that).

    Unfortunately I don't think adding this code (in a Document_New macro) to our existing templates is going to be a solution at this point for various reasons, though if it comes down to it we may have to go that route if it works out.

    As it happens, the way our software does documents is that it calls up the .dotx template a user chooses, and then inserts macros into the resultant .docx (coming from a macros.txt file). These macros are then called to populate the doc with values from the database and other functions.

    So my first thought was to just have a line in those macros to call ActiveDocument.AttachedTemplate = "".
    But when that gets called in this context it throws an error stating:

    Run-time error '5947'
    Could not change document template.

    But it WILL let me execute a line (in the same spot) like "ActiveDocument.UpdateStylesOnOpen = True" so I can execute changes to template settings the way I want, but it won't let me just set the Attached Template.

    Any ideas why that error is popping up for me here. I can't seem to find any answers

    Wednesday, November 28, 2012 12:59 AM
  • There seems to be some confusion regarding templates, documents and macros.

    You refer to:
    the way our software does documents is that it calls up the .dotx template a user chooses, and then inserts macros into the resultant .docx (coming from a macros.txt file).
    but macros cannot be saved in a docx file. In the scenario I'm proposing, the dotx template you refer to would have to be a dotm template containing the Document_New macro.

    The error you're getting suggests your code is trying to update a template rather than a document (ie you've opened a template as a template instead of, perhaps, creating a document from it).


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by Quist ZhangModerator Wednesday, November 28, 2012 8:44 AM
    • Unproposed as answer by loneil Wednesday, November 28, 2012 6:46 PM
    Wednesday, November 28, 2012 1:18 AM
  • Hi,

    let me join this discussion, as I have exactly the same problem as loneil.

    I don't want to have any template reference in the created document, and as well I don't care about all losing capabilities which would come with this reference. The drawbacks of this reference are to big.

    The suggested macro is only a solution if our customers would use only those templates we deliver.
    In this case we could prepare those templates with such a macro.

    But this isn't the case. Our templates are only samples, and of course the customers wants to create their own templates, and don't want to deal with VB macros.

    So is there really no official setting, checkbox, or what else, in any optionsdialog, which is available for every user, to prevent word to save the absolute path to the template ?

    I can't believe it !

    Wednesday, November 28, 2012 4:39 PM
  • I get the template/document/macro paradigm. We're not saving macros in the docx, what we're doing is this:

    1)The program creates a new document based on the selected template.

    2)The macros are inserted into the document and run to populate data

    3)The macros are removed and the document is saved as a docx

    I'm definitely not trying to clear the AttachedTemplate in the template. I'm looking at it now (breakpointed at that line) in the resultant docx. When I step to the next line I get the aforementioned error.
    So I haven't opened it as a Template, this is happening in the docx.

    I get that adding the line to a macro in the template itself could work here but as I mentioned that is a difficult solution for us. One reason is that, as you described, this would require changing our templates to dotm files instead of dotx and there are reasons we specifically only allow dotx files to function as templates. Another is that with a very large customer base, updating all the templates would be a drag (though this is workable).

    Having said all that, I've managed to make this work by clearing the template link in the C# code that does steps 1-3 above.
    It's too bad if I have to go this way, as it requires a code release to fix, but that's not a huge deal. 

    Thanks for your help

    Wednesday, November 28, 2012 6:57 PM
  • Oh, and Fritz (or anyone else), if you're having the exact same problem as us, then the following could be helpful:

    -Registry key to set timeout time for attached template connection
    http://answers.microsoft.com/en-us/office/forum/office_2010-customize/template-loading-timeout-in-word-2010/df031858-2b1d-46fc-a460-69afc19ebed7

    -Powershell script to clear attached template link from existing documents
    http://www.oostdam.info/index.php/sectie-blog/51-scripting/322-empty-wrong-word-template-path

    -VB app to clear attached template link from existing documents
    https://cjwdev.wordpress.com/2010/04/07/edit-attached-templates-for-word-documents/

    I agree that it just makes sense that there would just be a Word setting that prevents the saving of the path for a given template. I'm surprised there isn't.

    Wednesday, November 28, 2012 7:06 PM
  • Your disbelief seems to be based on a lack of understganding of the fundamentals of Word, which include every document being based on a template. You can change which template is used, but you must have one.

    In any event, if your clients are using the templates you're creating, the documents thus created will point to the templates stored on their system - there should be no need for macros or anything else. Furthermore, if macro-enabled templates are stored in a trusted location, there should be no issues with people having "to deal with VB macros" - they'll never see them.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, November 28, 2012 10:15 PM
  • Without seeing your code and knowing when the code generating the error message executes and what document it's trying to access at the time, it's impossible to diagnose with any certainty.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, November 28, 2012 10:19 PM
  • Hi Paul,

    you may be right, that for me the fundamentals of Word are not yet visible.

    Anyway, when you write "You can change which template is used, but you must have one", please explain why it ist possible to delete the reference with no impact on the content and format of this document.

    There are hundreds of hints in forums, how the reference can be changed, all based on VB code where at the end the Attachement path of the document ist modified or even deleted. In most cases the reason is, because the original templates are moved to other locations due to restructoring of the server. Many of them complain about long loading times, because Word is trying to search for the template but couldn't find it at the end. In my case it's a concurrency conflict, because a datasource of a serial letter template is locked by Word, and therefor another user cannot use the same template as long as the other document is opened.

    As I wrote, word documents are sent by email every day by millions of users, and none of them will ever send the referenced template with it. And yet, all recipients can open the doc on their machines without losing formatting information (I do not talk about any macros, perhaps indeed stored in the template).

    Even if in this case, the document will refer automatically to normal.dot on the target machine, which is available for sure, the original template is not necessarily essential.

    So for what is the reference necessary when just viewing the document ?

    Wednesday, November 28, 2012 11:12 PM
  • Anyway, when you write "You can change which template is used, but you must have one", please explain why it ist possible to delete the reference with no impact on the content and format of this document.

    All that does is to change the template to 'Normal', so you still have one.

    Granted, access to a particular template may not be necessary for mere document viewing, but it does matter when one is trying to manage document editing, especially if you're trying to ensure Style consistency via the 'Automatically update document Styles' setting. As you note, macro dependencies are another area where it matters.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, November 29, 2012 1:28 AM
  • Hi loneil

    <<1)The program creates a new document based on the selected template.

    2)The macros are inserted into the document and run to populate data

    3)The macros are removed and the document is saved as a docx>>

    If macros are allowed at all, then why not put the necessary macros in a dotm template that's not attached to the document, but loaded as an Add-in (Application.Addins). Your program could load and unload it as required. And that same global template could also take care of changing the template attachment.

    This template could be distributed as part of your solution, which is probably less "tricky" to manage as an update than needing to change templates installed for use by the user on their machines.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 29, 2012 6:42 AM
    Moderator