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.
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.