Delete macro from word document RRS feed

  • Question

  • I have 1000 office documents with Macro. How can I delete all macro from office document using script.

    Please remember to mark the replies as answers or vote as helpful if they help.

    Thursday, November 24, 2016 5:04 AM

All replies

  • Are you sure all the documents contain macros? Word docx documents, for example, cannot contain macros; only docm and doc documents can contain them. Word docx documents, however, can use macros in their attached templates. The simplest way to remove a macro from a Word document is to save it in the docx format, then delete the docm or doc version of it.

    To convert docm & doc documents in a selected folder to the docx format, thus deleting any macros they contain, you could use a macro like:

    Sub ConvertDocuments()
    Application.ScreenUpdating = False
    Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
    strDocNm = ActiveDocument.FullName
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    strFile = Dir(strFolder & "\*.doc")
    While strFile <> ""
      If (strFolder & "\" & strFile <> strDocNm) And (InStr(strDocNm, ".docx") = 0) Then
        Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, _
          AddToRecentFiles:=False, Visible:=False)
        With wdDoc
          .SaveAs FileName:=strFolder & "\" & Split(strFile, ".doc")(0) & ".docx", _
            FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
          .Close SaveChanges:=False
        End With
        Kill strFolder & "\" & strFile
      End If
      strFile = Dir()
    Set wdDoc = Nothing
    Application.ScreenUpdating = True
    End Sub

    Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function

    Paul Edstein
    [MS MVP - Word]

    Thursday, November 24, 2016 5:21 AM