How to strip macros/project from a word document

G

Gary F

Is there a simple programmatic way in Word 2003 to strip a word document of
it's VBA/macro code. e.g., given a directory of word documents, I'd like to
run a VBA script that opens up each in turn, "strips" them of any VBA
Project/Macro code, then saves them back.

I've done some googling but haven't seen anything.

The background is that the system that produces these documents generates
them with macros that are useful when the document is first created, but
problematic if the document is sent to someone outside the organization.

thanks in advance
 
W

Wraithchilde

This is a good one. I know you can delete macros from the VBA editor, and you
can delete NewMacros from the macro organizer tab
(wdDialogOrganizerTabMacros) but how to do it in code... I'm stumped. I tried
a few things but no success. Unfortunately the Delete button on the macros
tab is disabled while you're recording a macro so can't find out that way.
 
H

Helmut Weber

Hi Gary,

save it as RTF.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Gary F

Hmm... I guess that would work. How would I do that from the "outside" in
vba/script.

e.g.:

Set doc as new File

' do something here to point doc to the current file

doc.SaveAsRtf ??

Is there something like that?
 
H

Helmut Weber

Hi Gary,

see:

http://word.mvps.org/faqs/macrosvba/BatchFR.htm

and have a look at this,
stepping through in single step mode [F8].

Sub Test401()
Dim SPth As String
Dim sDoc As String
Dim sRTF As String
SPth = "c:\test\*.doc"
sDoc = Dir(SPth, vbNormal)
While sDoc <> ""
Documents.Open sDoc
sRTF = Left(sDoc, Len(sDoc) - 3)
sRTF = sRTF & "rtf"
ActiveDocument.SaveAs sRTF, wdFormatRTF
ActiveDocument.Close
sDoc = Dir
Wend
End Sub

With very many documents, though,
things will gain another quality,
and a lot of error checking would have to be done.
For speed, you might open documents with visible = false,
or hide Word altogether.

Also, my advise would be,
not to use the same folder for the RTFs and the docs.
Though I did it here, in this sample.

Still further, dir() may loop endlessly,
if the docs you open contain some code,
which writes new docs.

I'm hoping, there are no subdirs to be processed,
otherwise, ...
well, can all be done.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
Top