Re: Saving VBA Code Best Practice

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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!



.



Relevant Pages

  • Re: AutoOpen not Executing When Document Macros Disabled
    ... Normal.dot is already opened and accessible, so the macros therein ... execute regardless of enabling macros within the document? ... another template has code in that template's Document_Open event... ... public routines and two private routines. ...
    (microsoft.public.word.vba.general)
  • Re: sin(10^300), ignorance, collusion. was Re: TILE64 embedded multicore processors - Guy Macon
    ... in practice *and* in theory) ... |> count routines for bisection, ... |> slower fall-backs for crap arithmetics. ... And I am saying that IEEE 754 is a major CAUSE of that attitude. ...
    (comp.arch)
  • Re: HLA and embedded controllers
    ... it has to test the return code in order to pass that exception back ... routines that wind up getting called in the middle of the call sequence ... same way with my design. ... which is stronger than MASM's recusive macros. ...
    (alt.lang.asm)
  • Re: Observations from an old timer playing with 64 bit numbers...
    ... I do prefer the ntoh64 version but beXXtoh or whatever it looks like others are using is ok to me too since 'net' is a pretty wide definition and not ALL protocols are big endian. ... It is far from being about general byte order macros (those are mainly the ... it is only about these network functions. ... % the same as the network order, routines are defined as null macros. ...
    (freebsd-net)
  • Re: AutoOpen not Executing When Document Macros Disabled
    ... so the macros therein execute. ... particular to documents based on the AttachedTemplate. ... public routines and two private routines. ...
    (microsoft.public.word.vba.general)