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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Macro from a document 2

Status
Not open for further replies.

Iamthestig

Programmer
Apr 30, 2008
38
GB
I have a number of word docs with macros which generate report data from sql. Our users generate these documents and then often email them to clients. Depending on the email recipients email server, the attachment can be blocked due to it containing macros. Is there a neat solution to sending the document without the macro? I have tested copying and pasting the text to a new document and closing the original, which is fine but the document name defaults to Document1 when I want it to have the original name but unsaved so it prompts to save to a different location to the original.
Hope this is clear.
Thanks,

John
 
You could save document in rtf format before sending it.

combo
 
Thanks combo.

I didn't know that would delete the macros.

Have a star. :)
 
I realize you already got an answer but I just wanted to add a couple more ideas.

If all of the macros are in modules (as opposed to in the ThisDocument object), then you can:
[tab]- Go to Tools > Macro > Macros.
[tab]- Select Organizer.
[tab]- Select all of the items in the left window.
[tab]- Select Delete.

But, as I said, this won't get rid of code in the document itself, such as a Document_Open event.

OR, and this might be especially good if you are sending the emails with a macro anyway, you can use the following (which I adapted from something I use in Excel - I think I got it from Chip Pearson's site):
Code:
Sub DeleteAllVBACode()
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim TargetDocName As String

    TargetDocName = '[The document from which you want to strip code]
    Set VBProj = Documents(TargetDocName).VBProject
                 
    For Each VBComp In VBProj.VBComponents
        If VBComp.Type = vbext_ct_Document Then
            Set CodeMod = VBComp.CodeModule
            With CodeMod
                .DeleteLines 1, .CountOfLines
            End With
        Else
            VBProj.VBComponents.Remove VBComp
        End If
    Next VBComp
End Sub
[!]2 NOTES[/!]: Before you can use the above code, you may need to do the following:
[tab]- In Word, go to Tools > Macro > Security. Go to the Trusted Publishers tab. Check the box beside Trust access to Visual Basic Project.
[tab]- In the VB Editor, go to Tools > References. Select Microsoft Visual Basic For Applications Extensibility 5.3.

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top