Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: Solved: Run macro stored outside a file?

  1. #1
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location

    Solved: Run macro stored outside a file?

    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

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You could put the macro in the Normal.dot template, that way it would be available to all word documents.

  3. #3
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location

    Run macro stored outside a file?

    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?

  4. #4
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by clhare
    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.

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    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

  7. #7
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by clhare
    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
    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.

  8. #8
    Out of curiousity, why is it better to keep normal minimal?

  9. #9
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    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.

  10. #10
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by wadiohead
    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
    Quote Originally Posted by clhare
    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.

  11. #11
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    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!

  12. #12
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by clhare
    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.

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by mdmackillop
    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.

  15. #15
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Howard
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  16. #16
    VBAX Regular
    Joined
    Aug 2005
    Posts
    79
    Location
    Can some one point me to where to find more information on setting up global templates.

    Thanks

  17. #17
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by SailFL
    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.

  18. #18
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  19. #19
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by SailFL
    Can some one point me to where to find more information on setting up global templates.

    Thanks
    Hi,

    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/as...267181033.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!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  20. #20
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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).
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •