Forum Discussion

D_Watson2185's avatar
D_Watson2185
Copper Contributor
Apr 07, 2025
Solved

attach a common code template document via a template document

(I suspect this has come up before, but I came up empty searching. Wrong keywords?)

TL;DR: how can I have a .dotx template that creates new documents with their AttachedTemplate set to a different .dotm file?

Longer read: I want to have several different "document templates" (e.g., productXmaintenance.dotx, productYmaintenance.dotx, productZmaintenance.dotx) sharing a common "code template" for that family of templates (e.g., productAnymaintenance_sharedcode.dotm) and set as AttachedTemplate in their derived documents.

I.e., each derived document should have AttachedTemplate of productAnymaintenance_sharedcode.dotm, not the template they are derived from. So, user opens productXmaintenance.dotx, gets a new Document1 that a) already has AttachedTemplate productAnymaintenance_sharedcode.dotm and b) automagically runs the code template document_Open method which, in turn, does some "fill-in-the-blank" stuff for field code setup.

The goal is to maintain the fill-in-the-blank code once in the "CodeTemplate.dotm", not multiple times in each "DocumentTemplate.dotx" (or .dotm, FTM).

I've been achieving the end result with the "template" documents being .docx with the correct shared .dotm AttachedTemplate. Obviously, these .docx templates do not behave like real templates and can be saved over, etc.

First, I tried setting the AttachedTemplate in a "DocumentTemplate.dotx", but that was disabled.

Next, I tried adding a document_Open event that sets 'ActiveDocument.AttachedTemplate = "pathto\CodeTemplate.dotm"' to the existing .docx "template" and saving that as a real Template "DocumentTemplate.dotm". When I open this .dotm, I get a new document "Document1.docx" created with AttachedTemplate set to the "DocumentTemplate.dotm", but its "DocumentTemplate.dotm" Open event does not get called. If I then save that "Document1.dotx", close, and reopen it, the AttachedTemplate "DocumentTemplate.dotm" open event gets called, and the AttachedTemplate is now set to the desired "CodeTemplate.dotm". The "CodeTemplate.dotm" Open event still does not get called. So, I save/close the "Document1.docx" a second time, with only the AttachedTemplate fixed. WhenI re-open that, finally the AttachedTemplate "CodeTemplate.dotm" Open event code gets called and the fill-in-the-blank code that was the whole point of this exercise gets executed. Obviously, three opens and two saves and closes is not the desired behavior.

Suggstions? Best practice?

  • If your template is loaded as a global template, its AutoOpen will never run.

    Give it a different name and call it from the templates you want to use the code. Otherwise it just sits there in the background.

    If you give it parameters , i.e. Sub MyCode(a,b) it will not show up in the macro listings available from the user interface but will be available to call from other macros. Application Run MyCode. The parameters could be used, but need not be when calling it. You can get specific as to project and module in calling it as well to avoid confusion over the name.

    I routinely have over twenty template add-ins loaded.

    I have my Ribbon template which contains a lot of code to be shared/used by my form document templates. They use the code but it does not reside in the document templates.

    I am a lawyer, not a programmer. In programming language, I think this is called running subroutines.

  • Charles_Kenyon's avatar
    Charles_Kenyon
    Bronze Contributor

    You can use code in any .dotm template in Word that is "loaded" as a global template.

    You can load a template using code to add it to the templates collection, but I generally just save the global template in the Word Startup Folder. Either way, it stays loaded until unloaded.

     

    • D_Watson2185's avatar
      D_Watson2185
      Copper Contributor

      I'll look into those alternatives.

      My kneejerk reaction is that the CodeTemplate.dotm stuff only applies to several template .dotx/.dotm templates used several times a year to create a handful of new files--or maintain those files. That code has zero meaning/use outside this context, so having it available in every document is not helpful at best. Also, it has an AutoOpen() routine that, when in the AttachedTemplate for a .docx, runs on every Open. If the same behavior occurs for all documents refencing this global template, I'd have to make that code smart enough not to run for all but the relevant documents. Also, I'd have to locate the CodeTemplate in one of these magic folders, in ways (OneDrive) so it appeared to Word in each of my Windows machines. Luckily it's not multi-user, but just for me.

      Maintaining the same 160 or so lines of code in multiple .dotm templates might be less bad. Or just doing what I'm doing now and using .docx (each with the common AttachedTemplate .dotm) as "template" documents and trying to remember first to Save As. Maybe I should add that to the AutoOpen event?

      I thought I'd posted a similar reply this morning, but it seems to have vanished, or been moderated?, into the ether.

    • D_Watson2185's avatar
      D_Watson2185
      Copper Contributor

      I'll check those approaches out.

      My kneejerk reaction is that this CodeTemplate.dotm stuff only has meaning in the context of handful of Template documents each used to create new docs several times each year. It's useless outside that context. Worse, because it has an AutoOpen routine, I'd have to make that smart enough to detect the templates or their derivatives and only run all of the fill-in-the-blank code for those documents. Finally, this template in magic folder stuff would have to be made work on all my Windows machines. (At least it's not a multi-user problem, just me.) Putting the code (165 lines at present) in, and maintaining it in, multiple .dotm templates might be a better alternative. Or I can just keep doing what I've been doing and using .docx documents as "templates" with Save As in lieu of .dotm.

      • Charles_Kenyon's avatar
        Charles_Kenyon
        Bronze Contributor

        If your template is loaded as a global template, its AutoOpen will never run.

        Give it a different name and call it from the templates you want to use the code. Otherwise it just sits there in the background.

        If you give it parameters , i.e. Sub MyCode(a,b) it will not show up in the macro listings available from the user interface but will be available to call from other macros. Application Run MyCode. The parameters could be used, but need not be when calling it. You can get specific as to project and module in calling it as well to avoid confusion over the name.

        I routinely have over twenty template add-ins loaded.

        I have my Ribbon template which contains a lot of code to be shared/used by my form document templates. They use the code but it does not reside in the document templates.

        I am a lawyer, not a programmer. In programming language, I think this is called running subroutines.

Resources