Forum Discussion
attach a common code template document via a template document
- Apr 09, 2025
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.
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_Watson2185Apr 08, 2025Copper 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_Watson2185Apr 07, 2025Copper 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_KenyonApr 09, 2025Bronze 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.
- D_Watson2185Apr 09, 2025Copper Contributor
Thanks for that reply! It got me re-thinking about lots of Word automation stuff that I never messed with much and none in probably decades.
I did refactor my solution into a .dotm stored in my Word Startup folder, and .dotm document templates with a very tiny VBA footprint of common code in every template--three subroutines, each of three lines, only one of which is executable. So that meets the maintainability criteria that sent me down this rabbit hole in the first place.
The one thing that could have made my solution more elegant would be a way to instantiate a object of a class that is defined in the code.dotm, in the document.dotm, and pass it across. Application.Run seems pretty limiting. Probably by design.