PDA

View Full Version : Multiple Templates



MarkWoodward
01-15-2008, 06:11 PM
Hi all,

Is there anything similar to 'include' or 'require' in VBA?
I'm helping setup numerous templates in word. The 'design' of each template is different (header/footer etc), but the majority of VBA is identical.

I've been racking my brain trying to work out how best to set this up.
At the moment I'm just repeating the code in each .dot file.

Yesterday however I thought I'd spend some time on trying to:
- store the code in a separate file
- link to it in the 'presentation' template.

I'm using a 'document template (docTemp)' for the presentation stuff (ie the visible layout of the template).

I'm trying to store all the VBA stuff in a 'global template (globTemp)' and link to it from the 'document template'. I think this is working. Whether it's the right way to go about it though is another matter!

Within docTemps Document_Open() method I'm using:


AddIns.Add FileName:="H:\globalTemp.dot", Install:=True


Which appears to be working:

(Looks like I can't post links yet (this is my first post) so I'll ask if you could look at deepinit.com/images/tempAndAddins.JPG

So it looks like I have both templates available. Alt-F8 shows the macros from globTemp which is good. However I can't seem to access them from docTemp!!

How do I access macros from one template from another?
Is this the best approach? If not, any suggestions?


cheers,


--
Mark

lucas
01-15-2008, 08:07 PM
Have you considered having all of your template files in one template.dot?
You can have a userform with radio buttons to select the template you wish to use. All of the code can reside in the same file this way.

If you're interested see this (http://vbaexpress.com/forum/showthread.php?t=13543) thread. Gerry outlines it for you starting with post #7 and the bottom of Post #11(example file)

There is more information in the thread but that is where the discussion about mult templates in one file begins.

MarkWoodward
01-16-2008, 03:24 PM
Hi Steve,


Have you considered having all of your template files in one template.dot?
You can have a userform with radio buttons to select the template you wish to use. All of the code can reside in the same file this way.

If you're interested see this thread. Gerry outlines it for you starting with post #7 and the bottom of Post #11(example file)

There is more information in the thread but that is where the discussion about mult templates in one file begins.


Thanks, I'll have to look into that approach.
All of the 'design' of each template takes place in headers and footers though, which might be a barrier?

cheers,

--
Mark

fumei
01-17-2008, 02:01 PM
Hi Mark,

One needs to be careful with terms. "Template" should be used in a very specific way. A template, with the context of Word, is a file with a .dot extension. While both have a .dot extension, conceptually, there are two types of templates.

1. a document template. These are used to clone themselves into new documents.

2. a code container template. These are global templates. They, generally, are not used to clone new documents, and should not be used for that purpose. They are used to hold code.

Document templates can, and often do, hold code as well.

The fact that the differences in document structure are in the headers is simply not relevant. Properly designed, a document template can create whatever header structure you like, according to requirements.

"The 'design' of each template is different (header/footer etc), but the majority of VBA is identical."

"I'm using a 'document template (docTemp)' for the presentation stuff (ie the visible layout of the template)."

"Within docTemps Document_Open() method I'm using:"

OK, if you are using a document template correctly, you would not be using Document_Open - as a user should NEVER be opening the template - but Document_New. Document_New is the procedure that fires when you clone a new document from a document template.

Yes, in Document_New - cloning a new document - you can certainly load a global template as a addin. This is, in fact, a good idea - depending on requirements of course.

The point being is that the different headers and footers can be done from ONE document template. A very simple demo template attached - DifferentHeaders.dot.

Unzip it, and double click it in Explorer. Or if you have placed it in a known Word file location, use File > New to get it to clone itself.

In any case, Document_New fires the display of a userform. On the userform is a combobox with four choices for the header.

Pick one, and click OK. THAT header is created for THAT document. This is a very simple demo. If the requirements call for DifferentFirstPage and say a logo on page 1, and no logo onother pages, just text....whatever... it does not matter.

You can, literally, do whatever you want.

Using the demo from the thread Steve linked to, you can combine things.

User selects LetterType_A: the text content is for LetterType_A, and the headers/footers are set up for THAT.

User selects LetterType_B: the text content is for LetterType_B, and the headers/footers are set up for THAT.

All from ONE template file.

fumei
01-17-2008, 02:05 PM
Regarding the access to macros, could you clarify this a bit? I am not quite following.

MarkWoodward
01-17-2008, 03:08 PM
WOW!!!
Gerry, thank you!
Very informative.


Hi Mark,

One needs to be careful with terms. "Template" should be used in a very specific way. A template, with the context of Word, is a file with a .dot extension. While both have a .dot extension, conceptually, there are two types of templates.

1. a document template. These are used to clone themselves into new documents.

2. a code container template. These are global templates. They, generally, are not used to clone new documents, and should not be used for that purpose. They are used to hold code.

Document templates can, and often do, hold code as well.

The fact that the differences in document structure are in the headers is simply not relevant. Properly designed, a document template can create whatever header structure you like, according to requirements.

"The 'design' of each template is different (header/footer etc), but the majority of VBA is identical."

"I'm using a 'document template (docTemp)' for the presentation stuff (ie the visible layout of the template)."

"Within docTemps Document_Open() method I'm using:"

OK, if you are using a document template correctly, you would not be using Document_Open - as a user should NEVER be opening the template - but Document_New. Document_New is the procedure that fires when you clone a new document from a document template.



I've been using Document.open in document templates and wondering why I'm being asked to save the template (much to learn).




Yes, in Document_New - cloning a new document - you can certainly load a global template as a addin. This is, in fact, a good idea - depending on requirements of course.

The point being is that the different headers and footers can be done from ONE document template. A very simple demo template attached - DifferentHeaders.dot.



Thanks very much for that Gerry! Above and beyond what I hoped for.
I'll have to play around with this!!

My only concern though is my VBA illiterate friend who's asking me to do this won't be able to implement new templates without knowing VBA. At the moment he 'designs' a new document template and asks me to "attach the VBA stuff to it". While I'm still around it's OK as I can incorporate the template design into the single template. But should I leave, noone here (several hundred staff) knows VBA! Anyway, neither your problem or mine. Maybe he will have to learn some!!!




Unzip it, and double click it in Explorer. Or if you have placed it in a known Word file location, use File > New to get it to clone itself.

In any case, Document_New fires the display of a userform. On the userform is a combobox with four choices for the header.

Pick one, and click OK. THAT header is created for THAT document. This is a very simple demo. If the requirements call for DifferentFirstPage and say a logo on page 1, and no logo onother pages, just text....whatever... it does not matter.

You can, literally, do whatever you want.

Using the demo from the thread Steve linked to, you can combine things.

User selects LetterType_A: the text content is for LetterType_A, and the headers/footers are set up for THAT.

User selects LetterType_B: the text content is for LetterType_B, and the headers/footers are set up for THAT.

All from ONE template file.


Once again, thanks heaps for your help.

cheers,

--
Mark

fumei
01-17-2008, 04:06 PM
"At the moment he 'designs' a new document template and asks me to "attach the VBA stuff to it". "

I have no idea what that means.

MarkWoodward
01-17-2008, 08:21 PM
"At the moment he 'designs' a new document template and asks me to "attach the VBA stuff to it". "

I have no idea what that means.

Sorry :) ,

He'll sent me a .dot file with a header and footer and NO VBA.

Because these templates are used to create documents that are part of an inhouse document control system, I then add VBA. Basically there's 3 folders these documents (Pathology Manuals) can be in:
1. Draft
2. Current
3. Archive

The VBA I attach does things like the following:

1. Determine documents location
2. If in Current or Archive, disable menus, add 'current' or
'archived' watermark
3. If in Draft, enable menus, remove watermark
4. In any of these directories restrict printing. Can only print from a
customised form. Again, watermarking the doc as 'authorised copy' or
'unauthorised copy' based on who's trying to print it and which folder
the documents in.
5. Disable saving if in current or archive directory. ie only documents in
the draft directory can be modified.



What I've been doing till now is simply copying modules from a previous template into the new one and tweaking if required. Most haven't needed tweaking which is why I was asking if there was a way to 'include/ require' functions.


cheers,

--
Mark

fumei
01-18-2008, 10:45 AM
Oh boy. Time for a global template.