Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I hide macros in a protected document?

Status
Not open for further replies.

BrianWen

Programmer
Jun 8, 2009
102
DK
I have a template, that needs to be executed from an external script, setting some fields in the process. The template is protected/locked and the external script makes a copy of the template (the usual way) and unprotects it.
Unfortunate thing is that a user can copy the contents of the macros to a new documents and do whatever they like with it, which is not good. Is there a way to block entering macros while the template/document is protected?
 
Nevermind, I just found out myself. Typical...

In the VB Editor, go to Tools -> Preferences -> Protection.
 



Brian,

Thanks for sharing your Eureka with all of us, rather than just announcing that you merely found a solution.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi BrianWen,

Just to answer the question you post header posed: "Can I hide macros in a protected document?", there are various ways of hiding macros (ie making them invisible to users).

You can hide the macros by prefixing their names with 'Private ', as in:
Private Sub MyMacro()
Alternatively, you can give the macro a dummy parameter, such as:
Sub MyMacro(Optional Dummy As Boolean)

You can even hide a module by placing the following code at its top:
Option Private Module



Cheers
[MS MVP - Word]
 
And here's another way:

Sub ToolsMacro()
MsgBox "Move along please, there's nothing to see here ..."
End Sub

This one works by intercepting the Tools|Macro command (including Alt-F8).


Cheers
[MS MVP - Word]
 
Thanks for the comments...

macropod, got one question. I understand both methods in how I'm supposed to do, but how does the first one work in reality?

I mean, declaring the subs/modules as private, how does that allow me to see them and no one else?
 
Hi Brian,
declaring the subs/modules as private, how does that allow me to see them and no one else?
Simple: having hidden them this way, the only way to see that they even exist is via the vbe and, if you've protected that (per your Eureka moment), that's enough to deter most users.


Cheers
[MS MVP - Word]
 
Oh, so it only hides them from the macro list in Word itself? - I want to disallow access to them or hide them in the VBE too.

So locking the project worked perfectly...But it's always nice to learn new things anyways, so thanks for the tips!
 
Hi Brian,

If the user can't see them in the list, there's no way to select them for running. The only way around that wouldbe to call the macros from, say, another document and for that you'd need to know what the macro names are and their arguments (if any are required). The 'Option Private Module' makes this even harder to do - see the vba help file.


Cheers
[MS MVP - Word]
 
The is another way, which I use.

I have a global template which is NOT loaded on startup - i.e. it is NOT in the Startup folder.

However, there IS in the Startup folder a wee .DOT file that does one thing, and one thing only. It loads the OTHER .DOT file (the global template code container), as an add-in.

The advantages of this are:

1. the global code container template can be in any location (mine is on a network drive, and being one file can be maintained better);

2. template code containers loaded as add-ins are NOT - repeat NOT - viewable in the VBE.

In other words, all the procedures (and I have hundreds in there) are avilable to use, but are not available to view. If you click in the template project (which does show) in the VBE, you get a "Project unviewable." message.

A Project is only viewable if the file itself is open, and as an add-in it is NOT open.

Further, the global can use specific toolbars, menus etc (pre-2007) and these will be loaded.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top