PDA

View Full Version : Solved: Run macro stored outside a file?



clhare
03-10-2005, 10:35 AM
Is it possible to have a macro in a Word file that will then call macros stored outside the file?

I need to set up a number of different files that will actually use the same macros. If I can keep all the main macros somewhere else and just have in my Word files a simple macro that can call the main macros, it would be alot easier for me to make macro updates if they become necessary (I don't have to update the macro in every file--just in the outside location!).

Any help is greatly appreciated!

Cheryl
:dunno

Jacob Hilderbrand
03-10-2005, 10:42 AM
You could put the macro in the Normal.dot template, that way it would be available to all word documents.

clhare
03-10-2005, 11:16 AM
I know I wouldn't be allowed to add it to the Normal.dot file. To put it in another template in the Startup folder would mean that I'd have to get each potential user to copy it to their PC. If I'm not aware of all the users, then it's a potential problem making the macro updates available to all.

Is it possible to store the macros in a different folder that all users would have access to and then call the macros from within my templates when the file is run?

Howard Kaikow
03-10-2005, 04:49 PM
I know I wouldn't be allowed to add it to the Normal.dot file. To put it in another template in the Startup folder would mean that I'd have to get each potential user to copy it to their PC. If I'm not aware of all the users, then it's a potential problem making the macro updates available to all.

Is it possible to store the macros in a different folder that all users would have access to and then call the macros from within my templates when the file is run?

First, macros do not belong in the Normal template. Normal template should have as little as possible.

Use global templates, usually in the Start directory.
Or put templates in a Workgroups directory.

If the users are each on a separate PC, there's no way to avoid copying the templates to each PC, unless they are networked and share the templates over the network.

TonyJollans
03-10-2005, 05:31 PM
Howard is correct - as little as possible should be in Normal. Separate global templates are much better. But whatever route you choose, in order to run software, users must have it on their computers (or have access to it on a network, but that can cause its own set of problems) - that goes without saying.

It would be possible, to have code on individual machines that, once it had been installed once, routinely checked to see if there were updates available and, if so, applied them - or advised users that they were there waiting for them to apply. So your distribution problem doesn't have to be a permanent headache.

clhare
03-11-2005, 05:03 AM
I could put the Word template #2 (the one with all the macros) in a specific location, and everyone who would have access to the Word template #1 (the document file) would automatically have access to that location. That way, any edits to the macros would only need to be made in that one spot and all users would automatically get the updates when they run Word template #1.

What would I need to do in template #1 to call the macros in template #2?

Cheryl
:dunno

Howard Kaikow
03-11-2005, 08:06 AM
I could put the Word template #2 (the one with all the macros) in a specific location, and everyone who would have access to the Word template #1 (the document file) would automatically have access to that location. That way, any edits to the macros would only need to be made in that one spot and all users would automatically get the updates when they run Word template #1.

What would I need to do in template #1 to call the macros in template #2?

Cheryl
:dunno

The proper way to do things is to have a minimal Normal template. For example, in my Normal template I have only two modules.

Each module consists of nothing more than macro stubs that call the real code, which is, in my case, in a DLL, but it could just as well be in another template.

In addition, I use two global templates.

One has code for my customized menus and toolbars.
The other has whatever else I wish to have available globally.

Both global templates are in Word's Startup directory, so the macros are automatically made available no matter what is the attached template.

In my case, th real code is actually in two DLLs, but you need not do that.

Just place your templates in Word's startup directory and your users will be able to do whatever.

How you distribute/update these templates is a separate issue.

Unless all the computers are networked, users will have to check periodically for updates or an administrator will have to periodically update each system.

wadiohead
03-11-2005, 08:42 AM
Out of curiousity, why is it better to keep normal minimal?

clhare
03-11-2005, 09:14 AM
Is it possible to put the other template (with the macros) in a folder other than the Startup folder?

If it is possible, how would I call the macros that reside in another file? This would be my preferred approach. People move around from team to team alot here, and if I didn't have to use the Startup folder as the location for the actual macros, it would be alot easier. Since each team would be given access to a specific folder when they join the team, I would just add their macros file somewhere in that folder. Then I can just update the folder in that one location and they'd automatically have access to the updated macros.

Howard Kaikow
03-11-2005, 09:39 AM
Out of curiousity, why is it better to keep normal minimal?

e.g.

1. The larger is the Normal template, the longer it takes Word to startup.
2. App specific stuff should be in its own template, instead of cluttering up Normal, or global, templates.
3. Normal template should be read-only to prevent attacks by viri.
4. If Normal template is read-only, you can just ignore unexpected changes to Normal template caused by badly written apps and macros.

Below are the only two modules in my Normal template in Word 2003, where WordVBNormal is a class in a registered DLL. All other code is in global templates, attached templates or other DLLs.

With this setup, I can run with Normal as read-pnly, except when I momentarily have to reset the reference to the DLL when I change the DLL.

Note that the ResetToolsOptionsView macro is in Normal ONLY because there is a timing issue when Word starts and the macro, as currently coded, will not run from a global template (I may try to eliminate this restriction some day).



Option Explicit
Public clsWordVBNormal As WordVBNormal

Public Sub AutoClose()
SetupClass
clsWordVBNormal.AutoClose
End Sub

Public Sub AutoExec()
SetupClass
clsWordVBNormal.AutoExec
End Sub

Public Sub AutoExit()
SetupClass
clsWordVBNormal.AutoExit
End Sub

Public Sub AutoNew()
SetupClass
clsWordVBNormal.AutoNew
End Sub

Public Sub AutoOpen()
SetupClass
clsWordVBNormal.AutoOpen
End Sub

Private Sub SetupClass()
Dim docTemp As Word.Document
If clsWordVBNormal Is Nothing Then
If Documents.Count = 0 Then
Set docTemp = Documents.Add(Visible:=vbFalse)
End If
Set clsWordVBNormal = New WordVBNormal
clsWordVBNormal.SetClass Word.Application
If Not (docTemp Is Nothing) Then
docTemp.Close
Set docTemp = Nothing
End If
End If
End Sub




Option Explicit

Public Sub ResetToolsOptionsView()
clsWordVBNormal.ResetToolsOptionsView
End Sub



Is it possible to put the other template (with the macros) in a folder other than the Startup folder?

If it is possible, how would I call the macros that reside in another file? This would be my preferred approach. People move around from team to team alot here, and if I didn't have to use the Startup folder as the location for the actual macros, it would be alot easier. Since each team would be given access to a specific folder when they join the team, I would just add their macros file somewhere in that folder. Then I can just update the folder in that one location and they'd automatically have access to the updated macros.

global templates can be anywhere, but then you have to make sure that you set them up to load as global.

those in the Startup directory load automatically.

if you want each time to have access to different sets of global templates, then the AutoExec maro could be used to load/unload the appropriate templates for each team

Also, you could have a separate Startup directory for each team and use Tools |Options | File Locations to identify the appropriate Startup directory for each team member.

clhare
03-24-2005, 04:44 AM
I like the idea of the AutoExec macro instead of the Startup directory. That sounds like the best option for my situation. I will look further into that as I've never used AutoExec before and am not familiar with it. Thanks!

Howard Kaikow
03-24-2005, 06:17 AM
I like the idea of the AutoExec macro instead of the Startup directory. That sounds like the best option for my situation. I will look further into that as I've never used AutoExec before and am not familiar with it. Thanks!

Thte idea is to include the AutoExec macro within a template in the Startup directory.

In general, AutoExec macros do not work outside of the Normal and global (e.g., those in the Startup directory) templates.

mdmackillop
03-24-2005, 06:29 AM
Hi Howard,
Global templates are things I've never used, but they sound the way to go for all my Word routines, particularly in a networked environment where I have to make the changes. This subject would make a great Article for the site, as we seem now to advise users to store general macros in Normal.dot as a matter of course.
Is the same procedure suitable (or advisable) for Personal.xls?
Regards
Malcolm

Howard Kaikow
03-24-2005, 11:54 AM
Hi Howard,
Global templates are things I've never used, but they sound the way to go for all my Word routines, particularly in a networked environment where I have to make the changes. This subject would make a great Article for the site, as we seem now to advise users to store general macros in Normal.dot as a matter of course.
Is the same procedure suitable (or advisable) for Personal.xls?
Regards
Malcolm

Any decent Word user level book would already include a recommendation to minimize use of the Normal template.

For Excel, I have no need for lots of macros, so most everything is in individual workbooks. Very little in Personal.xls.

mdmackillop
03-24-2005, 11:59 AM
Thanks Howard

SailFL
08-09-2005, 12:02 AM
Can some one point me to where to find more information on setting up global templates.

Thanks

Howard Kaikow
08-09-2005, 05:52 AM
Can some one point me to where to find more information on setting up global templates.

Thanks

For info on where to save the global templates, look up "global templates" in Word's Help.

TonyJollans
08-09-2005, 05:55 AM
I have a short write-up on this. It is not yet really available for public consumption as it is part of a greater work but I don't think I'd be giving anything away if I were to send you a copy in confidence.

If you e-mail me - click on my handle and select e-mail from the dropdown - I will cut it and send it to you. Be aware when you read it that some references may not make complete sense out of context but it should still give you an introduction.

MOS MASTER
08-09-2005, 10:09 AM
Can some one point me to where to find more information on setting up global templates.

Thanks

Hi, :yes

I'm sure tony will set you up quite nicely. (Don't forget the help tip from Howard as well)

Google is a great resource on this topic as well.

Here's some info from MS:
http://office.microsoft.com/en-us/assistance/CH010267181033.aspx

The subject can never be captured in one tutorial cause questions will always remain.

Please come back with your specific questions on this matter! :whistle:

TonyJollans
08-09-2005, 05:03 PM
Hi SailFL,

Thanks for the e-mail. In it you say you want to do this in Excel, not Word.

Word and Excel work in rather different ways and knowing how to create a global template in Word isn't going to help you do the equivalent in Excel, I'm afraid.

This is the Word Forum, and this was originally a different thread, so very briefly the easiest thing to do in Excel is:

Select Tools > Macro > Record New Macro from the Menu
In the Store Macro In dropdown in the dialog, select Personal Macro Workbook and press OK
Click the Stop Recording button on the Stop Recording toolbar (if it's not visible, Select Tools > Macro > Stop Recording from the Menu)

Close Excel.
You will be prompted to save your Personal Macro Workbook. Say Yes. Excel will save it where it needs to be. You will, of course, be prompted to save anything else you've done to other workbooks - just treat those prompts as normal.

Next time you use Excel you will have your own Personal.xls file available (actually it was created when you said you wanted to record into it, but now it has been saved in the right place and will always be available to you).

MOS MASTER
08-10-2005, 02:20 PM
Related (Cross) post in Excel forum so we don't double answers: :yes

http://www.vbaexpress.com/forum/showthread.php?t=4525