PDA

View Full Version : A macro to import a macro?



kcguy
05-11-2009, 01:27 PM
I know it sounds weird, and maybe I am asking for the wrong thing, so let me explain the problem:

I have more than a hundred templates that all use the same modules and userforms for logic. A few months ago, everything was working just fine, and then they wanted a minor change. No big deal - I had it done in 30 minutes. Updating all of the templates took me hours.

Is there a way I can set these templates up so that they all import or refer to a single set of macros and forms?

Tommy
05-11-2009, 02:05 PM
:hi: Hi kcguy,

Welcome to VBAX

I would suggest that you place all of the macros and forms into a template and have all of your documents refer to it. Have the new template on a server so everyone can be updated when you copy the updated template to the server. You will need to update all of the existing templates, which may take hours now but minutes later. :)

kcguy
05-12-2009, 05:45 AM
So you're saying that a document can be created from 2 templates - one with the text, formatting, etc, and another with the code? How would I do that?

kcguy
05-12-2009, 05:54 AM
And thanks for the welcome!

lucas
05-12-2009, 06:30 AM
As Tommy has indicated, you can put all of your macro's that you wish to run on any open document into a template or .dot file. The code needs to refer to the "active" document, not thisdocument. Thisdocument would be the template.

put the new code container template in your word startup folder and it will be loaded any time Word is run and available to use on any open document.

Find your startup folder in Word by going to Tools-Options-File locations.

The code can be used on a document created from a template which as you say would contain the formatting etc.

Tommy
05-12-2009, 06:46 AM
The reason I suggest that the template be placed on the server is because you will have to go to each person's computer to copy the file or you will need to place the template in a login script to update each one's computer. This is of course with the assumption that there are multiple users. Otherwise if you are the only person place it in the startup directory as Steve has suggested.

Hi Steve! Long time no see!

lucas
05-12-2009, 07:16 AM
Hey Tommy, you are right about the network situation.....all well down south?

Tommy
05-12-2009, 07:52 AM
HOT! Trying to keep me and 3 other guys busy keeps me occupied so I don't get in trouble. :)

How are things up there?

lucas
05-12-2009, 08:10 AM
Glad to hear you are doing well Tommy. Things have slowed to a dead stop here but you can always find something to do if you keep looking.

kcguy
05-13-2009, 07:22 AM
Sorry for the late reply - I'm up to my eyebrows in work! I think I understand what you are recommending, and I think that I should have fed you a bit more info...

On autonew, a form is shown that asks the user to enter some info that is then used to select a proper header and footer(there are about 40 footer choices and 10 header choices - each document could have any combination of those headers and footers).

Many documents either have no header and footer or a preselected header and/or footer that shouldn't be changed, so I can't have my macros run on every document that the company uses. I can't have all of the company's documents refer to it.

I'm hoping that there is some method of

If referringtemplate.location = n:\templates\templatesthatneedheaderfooter then
Import frmHeaderFooterEntry
Import HeaderFooterModule
frmHeaderFooterEntry.show
endif

lucas
05-13-2009, 07:32 AM
You're not understanding how it works kcguy. The macro's in a global template are available for any file that is open in word. That does not mean that they all run each time or ever unless you specifically call them.

You can have a menu that is created in the global that will always show in any Word document that is open but won't run unless you select it from the menu to run on the active document. It is the way Word is designed and it is very useful once you understand how it works.

Your header/footer code could also be placed in the global and called when you start a new document from a template and want to set the header/footer.

Again. The code in a global template as described does not run unless you tell it to. It is available to all documents and runs when you call it on the active document only.

Autonew is old school and you should be using document new in the thisdocument module unless you are running Office 97......

Tommy
05-13-2009, 07:46 AM
An add-in maybe? With toolbars?
A different approach from what we have been recommending but this is starting to look like what is needed.

kcguy
05-13-2009, 08:05 AM
I think I'm finally picking up what you're putting down. I make a template with my form and modules and just dump it in the users' startup folders. I can just modify the login script to make the copy. (We want to allow the users to modify normal.dot, so we have to keep the startup folder on the individual PC's.)

I'll have a small bit of modification to start the form, but no more than what I already have to do for the latest change. Any future changes can be made once to one template and then copied out.

By the way - I'm used to being wrong and not understanding. I'm married!

lucas
05-13-2009, 08:11 AM
Hi Tommy, I use my globals like this and I guess it is technically an addin when used in this way isn't it?

For kcguy, adding the menu to your global is very easy:

http://slucas.virtualave.net/Wink/CustomMenuItem.htm

once you have your macro's in your template(global), save it as a .dot file in the startup directory and your macro's are readily available anytime you run Word.

lucas
05-13-2009, 08:20 AM
I think I'm finally picking up what you're putting down. I make a template with my form and modules and just dump it in the users' startup folders. I can just modify the login script to make the copy. (We want to allow the users to modify normal.dot, so we have to keep the startup folder on the individual PC's.)

I'll have a small bit of modification to start the form, but no more than what I already have to do for the latest change. Any future changes can be made once to one template and then copied out.

By the way - I'm used to being wrong and not understanding. I'm married!

You are starting to understand and you will really like it once you get your head wrapped around it.

Couple of notes: I think you should avoid using normal for anything such as a code container. Many things happen with the normal.dot besides code and it often becomes corrupt and unusable.......it can be frustrating. That is why it is advisable to use a global template/addin instead. It isn't affected by daily operations of Word and it is easy to backup and update.

You can put it in one location on a network and have everyone point their startup directory to that location so you only have to deal with one copy also if that is possible in your company.

As far as being wrong and married.....you're preachin to the choir with me and Tommy. We know exactly what you mean. Just hang in there long enough to get it working and you won't regret it.

kcguy
05-14-2009, 07:25 AM
Thanks for the help guys!