Re: Saving VBA Code Best Practice
- From: "Shauna Kelly" <ShaunaKelly@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 1 Mar 2007 19:37:17 +1100
Hi Dan
I think the following is usual, although others may have different
practices:
1. In the ThisDocument class, only put code that responds to the document
events such as Document_New, plus, perhaps, any routines that are called by
code in the document events, and not used elsewhere in your file -- and mark
those other routines Private.
2. Put all other code in one or more Modules. You can insert a new one using
Insert > Module and you can re-name it using the Properties window.
Good practice is to have only a very few Public Subs in each module (note
that in VBA all routines are public by default). There are any number of
exceptions to that, but it's the general rule. Any supporting routines
should be marked Private. And, good practice is to group routines into
Modules in some organized way. So, for example, if you have a couple of
routines that work on tables, and one that works on headers and footers,
then create two modules, name them (eg) Tables and HeadersAndFooters, and
put the routines in the appropriate module.
As you learn more, you might also use UserForms and Classes. You can insert
them using the Insert menu.
3. You will only see a macro at Tools > Macros > Macros if it:
- is in a Module (not in ThisDocument, or a UserForm or a Class)
- is a Sub (not a Function)
- is not Private
- does not take any parameters
- is not in a module prefaced with Option Private Module
In this way, you can determine which macros are exposed to the user, and
which are hidden.
Hope this helps.
Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
"Dan" <textheads@xxxxxxxxx> wrote in message
news:1172690986.228586.67390@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,
I'm new to VBA coding for Word and a little unsure of the best method
of saving my subroutines in a template. I would like to distribute
this template to internal employees (technical writers).
1. I wrote a couple subroutines that perform different actions. I put
all of them in the ThisDocument node under TemplateProject. Is this
standard practice?
2. I noticed that the subroutines appear in the Macros dialog
(Tools>Macro>Macros) but not in the Organizer. I imagine this is OK,
but what if I want to move the subroutines to a different template
down the road?
3. Also, how can I call my subroutines directly from Word? Should I go
through a separate procedure to create a new Macro that calls them?
Thanks for any guidance!
.
- Follow-Ups:
- Re: Saving VBA Code Best Practice
- From: Dan
- Re: Saving VBA Code Best Practice
- Prev by Date: Re: From This to this Please
- Next by Date: Re: duplex macro
- Previous by thread: Re: WordBasic.SelectSimilarFormatting
- Next by thread: Re: Saving VBA Code Best Practice
- Index(es):
Relevant Pages
|