The cleanup macro does not need to be copied, then deleted. There is no requirement that the code has to be in the document that it is performing actions on.
Code:
Dim ToBeCleaned As Document
Set ToBeCleaned = [i]document_name[/i]
With ToBeCleaned
.yadda yadda
.yadda yadda
End With
will do the job just fine. As long as the document to be cleaned is explicitly identified, then you can do whatever you want to it from another template.
It seems to me that copying and then deleting macro code is a messy, and potentially failing, way of doing things.
Heck you can have the cleanup code in the separate template and run it as a looping cleanup through a whole folder, never copying the code at all.
Code:
Sub CleanUp()
With ActiveDocument
' do this
' do that
' do aome other thing
End With
End Sub
Sub GetEachDoc()
Dim sPath As String
Dim CleanFile
sPath = "c:\test\"
CleanFile = Dir(sPath & "*.doc")
Do While CleanFile <> ""
Documents.Open FileName:=sPath & CleanFile
Call CleanUp
ActiveDocument.Close wdSaveChanges
CleanFile = Dir
Loop
End Sub
The above code can be in the separate template. It has the cleanup code as a separate procedure. The getting procedure uses Dir to process through all .doc files in a folder (in this case C:\test), passing each one to the cleanup procedure, then saving the changes.
This also can demonstrate something relevant to your last sentence.
there was a way to hide the code but still make it available for people to use.
Global templates. Global templates are never viewable - with the ONE exception of normal.dot.
Say you have your code in Corporate.Dot. It lives on a network folder. It is read-only...OS level read-only.
Two things you can do.
1. Have code in the user's normal.dot, OR code in documents that load corporate.dot as a global template. All code in corporate.dot is available, but the the project is unavailable to view. Projects are only available to view (except for normal.dot) when the file containing the project is
open.
However, CODE in a global template is completely executable even though the file is NOT open. This is what globals are for!
2. Using whatever means you like, you can copy the global corporate.dot to the user machine, and have it load on startup. Again, the CODE will be completely accessible, but unless the
file is actually opened, the code is unviewable - whether it is VBE protected, or not.
To have it be a startup global by user, put it in the Documents and Settings/
username/ApplicationData/Microsoft/Word/Startup folder. This will load the gloabl on startup for that user.
To have it be a startup global for any user of that machine, put it in Program Files/MSOffice/Startup...or whatever the startup folder is for your version.
Again, CODE in a loaded global is completely available, including any shortcut keys, custom toolbars etc, etc. Unless the actual file is also opened, the code is not viewable.
With careful use of user permission levels, OS level read-only attributes, you can load that global and make code
execution available, but code
access not.
Again, though...this is not bullet proof. Nothing is bullet proof.
This is the route I take. The only drawback across a network is either pushing down the global to each machine, so it is local (and therefore potentially accessible); or using other code to load it temporarily and keeping the actual file locked up on a server.
The interesting thing about globals is if you do NOT want it loaded on startup, the global file can be...anywhere. It can be...anywhere. Including a folder that is normally not visible to the user. If the user has READ rights to the folder, it can be loaded. If they do not have MODIFY rights, then they can modify, and the global is mostly safe.
You certainly do NOT need to copy macro code to the documents in order to execute it. There may be other reasons why you are doing this, but being able to execute code is not one of them.
Gerry
My paintings and sculpture