Detect and Remove VBA Macros from Word Document in C#

With Spire.Doc, developers can quickly detect if a Word document contains VBA macros and remove all of the VBA macros from a word document. This article is going to show you the detail steps of how to detect and remove VBA macros from word document using Spire.Doc.

Detail steps:

Step 1: Initialize a Document object and load the Word document.

Document document = new Document();
document.LoadFromFile("Input.docm");

Step 2: If the document contains Macros, remove them from the document.

if (document.IsContainMacro)
{
    document.ClearMacros();
}

Step 3: Save the document.

document.SaveToFile("Output.docm", FileFormat.Docm);

Full code:

using Spire.Doc;

namespace Remove_Macros_from_Word
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize a Document object
            Document document = new Document();
            //Load the Word document
            document.LoadFromFile("Input.docm");

            //If the document contains macros, remove them from the document
            if (document.IsContainMacro)
            {
                document.ClearMacros();
            }
            
            //Save the document
            document.SaveToFile("Output.docm", FileFormat.Docm);            
        }
    }
}