locked
VBComponent.Remove and CodeModule.DeleteLines not working for Word.Application.VBProject RRS feed

  • Question

  • Hey folks,

    I'm trying to remove all macro's from office documents. VBComponent.Remove and CodeModule.DeleteLines works great for Excel and PowerPoint. However, for Word documents it just doesn't delete the component or the lines. I get no errors, and it looks as if it works, but when I SaveAs a new file, the macros are still there.

    Is there some Word specific reason this might not work, or something else? This isn't my forte, so any help is greatly appreciated.

    Thanks

    Tuesday, June 5, 2012 6:18 PM

Answers

  • Hi Peter, thanks for the quick responce. I've used that code from cpearson's site as well :) So... I'm dumb and realized the issue. At some point in time when viewing the list of macros in word I selected to "view all macros in " -> "all active templates and documents", and not just the current file being viewed. So the macros in word are no exception, and they were being deleted just fine, I was just looking at all of them by accident and not just the ones in the opened file.

    I appreciate the help!

    Thanks

    • Marked as answer by rmadair Tuesday, June 5, 2012 8:02 PM
    Tuesday, June 5, 2012 8:01 PM

All replies

  • Without seeing your code it's not possible to suggest why it doesn't work, but have a go with this (VBA in the same Word instance)

    Sub RemoveDocCode()
    Dim sDocName As String
    Dim vbp As Object ' VBProject
    Dim vbComp As Object ' VBComponent
    Const vbext_ct_Document As Long = 100&
         sDocName = "mtDoc.doc"    ' change
         Set vbp = Documents(sDocName).VBProject
         For Each vbComp In vbp.VBComponents
                 If vbComp.Type = vbext_ct_Document Then
                         cnt = vbComp.CodeModule.CountOfLines
                         If cnt Then
                                 vbComp.CodeModule.DeleteLines 1, cnt
                         End If
                 Else
                            vbp.VBComponents.Remove vbComp
                 End If
         Next
    End Sub

    Peter Thornton

    Tuesday, June 5, 2012 7:16 PM
  • Hi Peter, thanks for the quick responce. I've used that code from cpearson's site as well :) So... I'm dumb and realized the issue. At some point in time when viewing the list of macros in word I selected to "view all macros in " -> "all active templates and documents", and not just the current file being viewed. So the macros in word are no exception, and they were being deleted just fine, I was just looking at all of them by accident and not just the ones in the opened file.

    I appreciate the help!

    Thanks

    • Marked as answer by rmadair Tuesday, June 5, 2012 8:02 PM
    Tuesday, June 5, 2012 8:01 PM