Avatar of Nobuo Miwa
Nobuo Miwa
 asked on

Remove Office macro by VC++

Hello Experts,

I want to remove macro in MS Office(Excel,Word,PowerPoint) file by VC++.
Do I need to work with C#  with .NET ?

I want to delete all visible and invisible macros from Office 2003, 2007 files.

Can someone suggest how to code removing macro ?

Nobuo Miwa
Microsoft ExcelMicrosoft WordMicrosoft OfficeVB ScriptVisual C++.NET

Avatar of undefined
Last Comment
Nobuo Miwa

8/22/2022 - Mon
Anastasia D. Gavanas

Microsoft has already done this and is described here for Word files (DOCM extension).
Take a look, it should work just fine and then you can tweak for other file extension.
Jamie Garroch (MVP)

You could save the macro-enabled file in the corresponding non-macro file format and the VBA project will be removed automatically.

For example:

Excel : .xlsm -> .xlsx
Word : .docm -> .docx
PowerPoint : .pptm -> .pptx
Anastasia D. Gavanas

This macro will do this for Word
Sub SaveAsDocx()
Dim file
Dim path As String
path = "C:\Test\"           'your folder here

file = Dir(path & "*.docm")
Do While file <> ""
Documents.Open FileName:=path & file
ActiveDocument.SaveAs2 FileName:=path & file, FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
        AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
        EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
        :=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
file = Dir()
End Sub

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Nobuo Miwa

Thank you guys,

Are those approach able to remove macro from Office 2003 ?

Nobuo Miwa
Jamie Garroch (MVP)

This will save a macro-enabled PowerPoint presentation in the .pptm file format without macros to the same folder without macros using the .pptx file format:

Sub SaveWithoutMacros()
  With ActivePresentation
    MsgBox "Saving " & Chr(34) & .Name & Chr(34) & " as : " & vbCrLf _
      & Replace(.Name, ".pptm", ".pptx") & " here : " & vbCrLf _
      & .Path
    .SaveAs FileName:="NoMacros.pptx", FileFormat:=ppSaveAsOpenXMLPresentation
  End With
End Sub

Open in new window

Jamie Garroch (MVP)

Office 2003 is pre-OpenXML formats and used the Office proprietary format which did not have separate file formats for macro-enabled presentations. Do you need to keep the 2003 format or can you save it in the newer XML format? Here is an example of a programmatic approach to remove code modules one at a time for Excel:

Option Explicit 
Sub DeleteVBA() 
    ' Trust Access To Visual Basic Project must be enabled:
    ' Tools | Macro | Security | Trusted Sources
    Dim lComp As Long 
    Dim lLine As Long 
    On Error Resume Next 
    With ActiveWorkbook.VBProject 
        For lComp = .VBComponents.Count To 1 Step -1 
            .VBComponents.Remove .VBComponents(lComp) 
        For lLine = .VBComponents.Count To 1 Step -1 
            .VBComponents(lLine).CodeModule.DeleteLines _ 
            1, .VBComponents(lLine).CodeModule.CountOfLines 
    End With 
    On Error GoTo 0 
End Sub

Open in new window

Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Anastasia D. Gavanas

Jamie's code should work for the other apps :)
Nobuo Miwa

Hello guys,

I want code that can run as standalone app , and does not run as macro(VBA).

> You could save pre-2007 files as macro-free .docx format and then open
> and save them in the older format.

I will try this from C# and post result on this thread.
Nobuo Miwa

Thanks GrahamSkan !

It works fine for me and easy to implement with C#.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck