Remove macro module using a macro

N

Neil Humphries

I have some macros in a Word document that I want to remove before the
document is circulated to avoid the recipients running the macros again. Due
to local network policy, the macros must be in the document and not in
Normal.doc.

A programming reference says the following code will work for Excel:
With ActiveWorkbook.VBProject
.VBComponents.Remove .VBComponents ("RFP_Macros")
End With

I can't get it to work in Word after changing ActiveWorkbook to
ActiveDocument.
Any suggestions?
 
J

Jonathan West

Neil Humphries said:
I have some macros in a Word document that I want to remove before the
document is circulated to avoid the recipients running the macros again.
Due
to local network policy, the macros must be in the document and not in
Normal.doc.

First of all, I can think of ways round that problem. Place the macros in
another template. The template can either be used as the attached template
for the documents, or can be loaded as an add-in. See this article for more
about this.

Distributing macros to other users
http://www.word.mvps.org/FAQs/MacrosVBA/DistributeMacros.htm
A programming reference says the following code will work for Excel:
With ActiveWorkbook.VBProject
.VBComponents.Remove .VBComponents ("RFP_Macros")
End With

I can't get it to work in Word after changing ActiveWorkbook to
ActiveDocument.
Any suggestions?


There is a simpler approach. Save the document as an RTF file. This strips
out all macros. Then close it, re-open it and then save again as a document.
(You have to close and re-open for this trick to work).

i suggest you run this as a pair of pbatch processes. First of all, save all
the documents in a folder as RTF files. Then save all the RTF files in the
folder as documents.

This article gives an example of how to do batch processes on a folder full
of documents.

Find & ReplaceAll on a batch of documents in the same folder
http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm

You will need to replace the code in the middle of the loop with your own
code that does the "Save As".
 
N

Neil Humphries

Jonathan West said:
First of all, I can think of ways round that problem. Place the macros in
another template. The template can either be used as the attached template
for the documents, or can be loaded as an add-in. See this article for more
about this.

Distributing macros to other users
http://www.word.mvps.org/FAQs/MacrosVBA/DistributeMacros.htm



There is a simpler approach. Save the document as an RTF file. This strips
out all macros. Then close it, re-open it and then save again as a document.
(You have to close and re-open for this trick to work).

i suggest you run this as a pair of pbatch processes. First of all, save all
the documents in a folder as RTF files. Then save all the RTF files in the
folder as documents.

This article gives an example of how to do batch processes on a folder full
of documents.

Find & ReplaceAll on a batch of documents in the same folder
http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm

You will need to replace the code in the middle of the loop with your own
code that does the "Save As".


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Those are good suggestions, but I don't know if I would be allowed to
implement them in my environment. The network is tightly controlled, not just
at this locally, but also from a central location in another city. It can
take a lot of effort just to get folder access permissions changed. It is a
pain, but with hundreds of PC's in dozens of locations I understand the need
to exercise control.

Only 1/10th of the people at this location will access the file in question.
It is in a server folder to which only 2 people have write access.

The users are not sophisticated. Most have not knowingly encountered a macro
before. The macros can currently be run either before or after the file is
'saved as' to a project folder.

I could force the user to 'save as' to the project folder first with an
auto-open macro. Then once they have finished editting and running the
macros, I could program a 'save as' .RTF and then as .docx and then delete
the .RTF. That's a lot more programming than I have done before.

I don't have access to the users PC's to put a template file in their
startup folder or template path and there is no way I would be allowed to
create a login script. (I did that on a Netware network years ago to
automatically update 50 PC's.) Changing the login script week by week as the
macros evolve would not find any support. IT is just to busy to worry about
what I may inadvertently do to the users or network.

So, I am back to trying to delete a macro module programatically as possibly
the simplest approach for my situation. Can it be done in Word with VBA, or
is it not allowed?
 
J

Jonathan West

Neil Humphries said:
Those are good suggestions, but I don't know if I would be allowed to
implement them in my environment. The network is tightly controlled, not
just
at this locally, but also from a central location in another city. It can
take a lot of effort just to get folder access permissions changed. It is
a
pain, but with hundreds of PC's in dozens of locations I understand the
need
to exercise control.

Only 1/10th of the people at this location will access the file in
question.
It is in a server folder to which only 2 people have write access.

The users are not sophisticated. Most have not knowingly encountered a
macro
before. The macros can currently be run either before or after the file is
'saved as' to a project folder.

I could force the user to 'save as' to the project folder first with an
auto-open macro. Then once they have finished editting and running the
macros, I could program a 'save as' .RTF and then as .docx and then delete
the .RTF. That's a lot more programming than I have done before.

I don't have access to the users PC's to put a template file in their
startup folder or template path and there is no way I would be allowed to
create a login script. (I did that on a Netware network years ago to
automatically update 50 PC's.) Changing the login script week by week as
the
macros evolve would not find any support. IT is just to busy to worry
about
what I may inadvertently do to the users or network.

So, I am back to trying to delete a macro module programatically as
possibly
the simplest approach for my situation. Can it be done in Word with VBA,
or
is it not allowed?

Generally not. It depends on the security settings of the PCs concerned,
specifically, the setting in the Security dialog "Trust access to the Visual
Basic Project".

If the systems are as thoroughly locked down as you suggest, then it is a
pretty safe bet that your users will not have that box checked, and will in
fact not be able to change the setting for themselves no matter what.

However, ij your latest post you have introduced a new element. You have
mentioned that you want to save as docx. Until now, I was assuming you were
running Word 2003, because it is simply not possible to put macros into docx
files - Word 2007 doesn't permit it. However if you are running Word 2007,
or if your final target file format is docx, then simply do a save as docx,
and the macros will be automatically stripped out.
 
N

Neil Humphries

Jonathan West said:
Generally not. It depends on the security settings of the PCs concerned,
specifically, the setting in the Security dialog "Trust access to the Visual
Basic Project".

If the systems are as thoroughly locked down as you suggest, then it is a
pretty safe bet that your users will not have that box checked, and will in
fact not be able to change the setting for themselves no matter what.

However, ij your latest post you have introduced a new element. You have
mentioned that you want to save as docx. Until now, I was assuming you were
running Word 2003, because it is simply not possible to put macros into docx
files - Word 2007 doesn't permit it. However if you are running Word 2007,
or if your final target file format is docx, then simply do a save as docx,
and the macros will be automatically stripped out.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
I appreciate your time and insight.

The final format is intended to be .docx. I just don't know how to determine
if any given save operation is the final save operation. (Same problem I have
with knowing when to delete the macros.) The users may open and save the
document several times before editting is completed. I think I may have to
rely on the users to double click a macro button to declare the editting is
complete.

We did manage to get the server folder the original documents are stored in
to be designated a trusted folder so the users don't have to enable macros
each time they open the original documents. I don't know if that would set
the trust levels needed.

If not, I will have to figure out how to force a save as and delete the
original file.
 
J

Jonathan West

I appreciate your time and insight.

The final format is intended to be .docx. I just don't know how to
determine
if any given save operation is the final save operation. (Same problem I
have
with knowing when to delete the macros.) The users may open and save the
document several times before editting is completed. I think I may have to
rely on the users to double click a macro button to declare the editting
is
complete.

We did manage to get the server folder the original documents are stored
in
to be designated a trusted folder so the users don't have to enable macros
each time they open the original documents. I don't know if that would set
the trust levels needed.

If not, I will have to figure out how to force a save as and delete the
original file.

I really would still strongly recommend that you find some way of putting
code in a template, separate from the document. Some thoughts which may not
have occurred to you.

1. Templates don't have to be in the templates folder. They can be anywhere,
and if you double-click on a template in Windows Explorer, the default
operation is to create a new document based on the template.

2. It is possible to digitally sign the VBA project in a template, and have
the computers which use the template be set to trust the digital
certificate. If you are only dealing with a small number of computers, it is
perfectly possible to self-generate the certificate. Look up" difital
Certificate for VBA projects" in the Help.

If you can get the template separate from the document, then it doesn't
matter whether you know if the surrent save is of the final version. The
document won't contain the code anyway, and can safely be saved as docx at
any time. It will contain the reference to the template containing the code,
and so have the features available. When you send the document outside the
organisation, the customers won't have access to the template and therefore
will not be able to run the features.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top