Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remove Office macro by VC++

Posted on 2016-08-30
10
Medium Priority
?
151 Views
Last Modified: 2016-08-31
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 ?

Regards,
Nobuo Miwa
0
Comment
Question by:NobMiwa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 18

Expert Comment

by:xtermie
ID: 41777660
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.
https://msdn.microsoft.com/en-us/library/office/gg188063.aspx
0
 
LVL 12

Expert Comment

by:Jamie Garroch
ID: 41777681
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
0
 
LVL 18

Expert Comment

by:xtermie
ID: 41777688
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
ActiveDocument.Close
file = Dir()
Loop
End Sub

Open in new window

0
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

Author Comment

by:NobMiwa
ID: 41777707
Thank you guys,

Are those approach able to remove macro from Office 2003 ?

Regards,
Nobuo Miwa
0
 
LVL 12

Expert Comment

by:Jamie Garroch
ID: 41777767
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

0
 
LVL 12

Expert Comment

by:Jamie Garroch
ID: 41777782
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) 
        Next
        For lLine = .VBComponents.Count To 1 Step -1 
            .VBComponents(lLine).CodeModule.DeleteLines _ 
            1, .VBComponents(lLine).CodeModule.CountOfLines 
        Next
    End With 
    On Error GoTo 0 
     
End Sub

Open in new window

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 41777821
You could save pre-2007 files as macro-free .docx format and then open and save them in the older format.
0
 
LVL 18

Expert Comment

by:xtermie
ID: 41777953
Jamie's code should work for the other apps :)
0
 

Author Comment

by:NobMiwa
ID: 41778946
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.
0
 

Author Closing Comment

by:NobMiwa
ID: 41779163
Thanks GrahamSkan !

It works fine for me and easy to implement with C#.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question