macropod said:
Hi Nisha,
You can restrict access to code in various modules by protecting them with a
password. The following code prevents someone from pressing the ALT F8 keys
to view/run the various subroutines. You put this sub in a file that you
want to protect. Then, if your user presses Alt+F8 or goes to
Tools|Macros|Macros... all they will get is a message box that says
disabled.
Sub ToolsMacro()
MsgBox "Disabled"
End Sub
Basically, this hijacks the built-in ToolsMacro Command.
This can be got round very easily, it is not a security feature.
You might also consider keeping the user from seeing the macros in the first
place, rather than taking away their ability to use the macro dialog.
Although you can't grey out the Edit button for macros that are in a file,
Of course you can. If the macros are in a template loaded as an add-in
rather than in the template attached to the current document, then the edit
button is greyed out. For more on distributing macros in an add-in, take a
look at this article
Distributing macros to other users
http://word.mvps.org/FAQs/MacrosVBA/DistributeMacros.htm
you can keep them appearing in the list of available macros. A few ways to
do that:
· Make them into Subs that take arguments (or Functions for that matter).
You can even do this using dummy arguments.
· Make them Private rather than Public Subs (only callable by other Subs in
the same Module).
· Put an "Option Private Module" declaration at the top of any code Module
housing Subs you don't want the user to see in the Macros list. These Subs
will be callable by any other sub in the same project, but not by any sub in
any other project.
Well I never! I learned something new today. I had never come across Option
Private Module. That might have some uses. Thanks!
Another approach would be to use an environment variable in the macro like:
If Environ("Username")<>Yourname Then Exit Sub
Although users could see the macro, this would effectively stop anyone
except you using it.
Those are some interesting ideas, but it seems that aren't addressing the
original question, which was how to prevent *editing* of the macros rather
than stopping them being run.