PDA

View Full Version : Using Global Template(s)



MWE
03-16-2006, 12:05 PM
I am trying to migrate from the use of Normal.Dot for macro storage to using one or more global templates. I have tried this in the past based on warnings that Normal.Dot often gets cranky, and ran into problems making that approach work. So I took the easy way out, continued to use Normal.Dot and ... I have now encountered a cranky Normal.Dot and wish to try again. I hope that the problems I have encountered in the past are just (yet another example of) my not knowing how to do things correctly:

1. I could not find a way to have a global template "open" (so I could fiddle with code) when anything else was open, e.g., the target word appl. A global template is "available" and in some sense opened when Word opens if the template is in the Word StartUp directory. But I can not do anything with it. Based on my exploration of how global templates worked, they are closed except when explicitly opened. Is there some way to have the template "open" automatically?

2. code that seemed to run fine when part of a code module in the parent word file or when in Normal.dot did not run when it was in a global template. I would get an error, but since the global template is closed, I could not find out anything about what proc had failed, what the problem was, where the error was, etc. I would put the "module" back in Normal.dot or the parent word file and the code ran fine (again). Is there some way to have the template "not hidden" or "available" or whatever the right term is?

3. code in the global template that used objects or functions from another MS appl library did not work; it was as though the global template did not recognize any dlls except those normally associated with Word even though they were explicitly referenced (VBAE|Tools|References...) in the word parent. This makes a little bit of strange sense if the "connectivity" of the global template is different from that of Normal.Dot

What I "want" is for the global template to work more like Personal.xls does in Excel.

I have not found a very useful source for info on how to use Global Templates and would appreciate any feedback on where I might look.

Thanks.

fumei
03-16-2006, 12:24 PM
1. No. Normal.dot is a special case. It is a global, and its modules ARE available all the time. All other globals are not. What happens is that if a global is loaded - as a global - Word parses the modules and make the procedures available for execution. The file is indeed not open. To work on the modules, the file itself must be opened, just like any other file that has a VBProject. Note that if you do have it open, and write code, this code is immediately available. The file does NOT have to be saved, although of course it always a good idea to do periodic saving....

2. It is always helpful to actually say what error you get.
I would get an errorNo, if the actual global template is not explicitly open, it is hidden. You must open the file. I have never had properly written code fail from a global template. It works.

3. Global templates make references that are in the global template. I have no idea what you mean by references in the word parent.
it was as though the global template did not recognize any dlls except those normally associated with Word even though they were explicitly referenced (VBAE|Tools|References...) in the word parentThe Word parent only has references that are built right into the executable. You can not make any new references within the Word parent. Can't be done. You make new references within a document or template. Those references only apply to that document or template. Global template handle references to other apps just like any other document or template that makes references. If they are in the global, then they are available.

TonyJollans
03-16-2006, 12:39 PM
1. To edit code in a global template it must be opened 'as a document'. There are several ways to do this; one of the easiest is to put this in the global template's ThisDocument module..Public Sub OpenMe()
Templates(Me.FullName).OpenAsDocument
End Sub You then have a mechanism to open it which is accessible via Alt+F8 and you can put it on a command bar if you want one click access to it. You can expand on this is any way you want; to make it appear like Excel's Personal.xls you could make its Window invisible but I have to say that Word does one or two funny things with invisible windows.

2. Hard to say what might be wrong - perhaps some sort of reference to a document that was not explicit enough.

3. Each template has its own set of references - it sounds like you were expecting references in Normal to be globally available. Any references you want in the global template must be explicitly set in it.

TonyJollans
03-16-2006, 12:42 PM
:) Must learn to refresh before posting.

'twas written so might as well be posted I suppose - and looks like we're both saying the same.

fumei
03-16-2006, 12:45 PM
Ah, moderately great minds think sort of alike.

Yes, it is a handy idea to have an easily opened global. I have an icon on the standard toolbar that opens my main global. So if I need to work on the code in it, it is a single click away. The macro both opens my global and switches me to the VBE.

MWE
03-16-2006, 01:19 PM
Thanks for the prompt and helpful reply

1. No. Normal.dot is a special case. It is a global, and its modules ARE available all the time. All other globals are not. What happens is that if a global is loaded - as a global - Word parses the modules and make the procedures available for execution. The file is indeed not open. To work on the modules, the file itself must be opened, just like any other file that has a VBProject. Note that if you do have it open, and write code, this code is immediately available. The file does NOT have to be saved, although of course it always a good idea to do periodic saving....
So, how to I open both the word doc that I want to work on and the global template which contains the libraries I need (and may want to tweak). Do I have to open the target doc and then manually open the global template? It would appear so. Is there some easier way than navigating the 48 layers to get to that file. Shortcuts do not work. A shortcut to the directory and then right click File|Open works. Seems like a lot of extra work for so simple and obvious a need. Nevermind ... answered in a subsequent reply (this would not happen if I could get the forum server to reply more quickly)


2. It is always helpful to actually say what error you get. I agree, but the message is simply "Compiler Error in Hidden Module: ModuleName" Nothing else. I suspect that problem is the references to other dlls (#3)


I have never had properly written code fail from a global template. It works.

3. Global templates make references that are in the global template. I have no idea what you mean by references in the word parent.The Word parent only has references that are built right into the executable. You can not make any new references within the Word parent. Can't be done. You make new references within a document or template. Those references only apply to that document or template. Global template handle references to other apps just like any other document or template that makes references. If they are in the global, then they are available. I should have stated "word parent doc" not "word parent" I think I now understand what might be wrong (or a piece of it) Assume that A.doc is a document that is open for some work. I want to run some procedure that is in a global template G.dot. G.dot resides in the Word StartUp directory so it is open but hidden. I have assumed that the G.dot would behave as just another code library, i.e., as long as A.doc has explicit references to the other dlls, the G.dot would automatically know and inherit those references. This is the way it works in Excel. So, I opened G.dot and explicitly added the reference to the "other" dlls, saved and closed G.dot. I then opened A.doc and tried to run a proc in G.dot that references or uses some xls objects. It worked. :clap::clap::clap:

So, I have ground quite a few teeth, but successfully got the vehicle moving. Now I want to shift from 1st to 2nd. Are there any special things I should know about global templates, for example: Several of the modules in G.dot have their own user-defined data types and their own global variables that are used in that module and in other modules and forms. Any problems with that?

fumei
03-16-2006, 06:01 PM
I want to run some procedure that is in a global template G.dot. G.dot resides in the Word StartUp directory so it is open but hidden. No. Again. I repeat. It is NOT open. G.dot is NOT open. A template file in Startup is loaded as a global template. Global templates, as I stated, are NOT open. Their procedures are parsed, and pointers to them are established. This makes the procedures available for execution. But the file itself is NOT open. That is why it is hidden. It is NOT open.
I have assumed that the G.dot would behave as just another code libraryWhy? It is not a code library.
So, I opened G.dot and explicitly added the reference to the "other" dlls, saved and closed G.dot. Which is exactly what I stated in my previous post....that is:
Any references you want in the global template must be explicitly set in it.