Search code examples
c#.net.net-corems-wordms-office

how to transfer information from (.docx format) to (.docm format) using .Net-Core?


How to convert from DOCX to DOCM ?

In this document there is a converting from docm to docx.

https://learn.microsoft.com/en-us/office/open-xml/how-to-convert-a-word-processing-document-from-the-docm-to-the-docx-file-format

can we do the opposite ( DOCX to DOCM ) ?


Solution

  •     public void ConvertDOCXtoDOCM(string fileName)
        {
            bool fileChanged = false;
    
    
    
            using (WordprocessingDocument document =
        WordprocessingDocument.Open(fileName, true))
            {
               
                document.ChangeDocumentType(
                    WordprocessingDocumentType.MacroEnabledDocument);
    
                // Track that the document has been changed.
                fileChanged = true;
            }
    
            // If anything goes wrong in this file handling,
            // the code will raise an exception back to the caller.
            if (fileChanged)
            {
                // Create the new .docx filename.
                var newFileName = Path.ChangeExtension(fileName, ".docm");
    
                // If it already exists, it will be deleted!
                if (File.Exists(newFileName))
                {
                    File.Delete(newFileName);
                }
    
                // Rename the file.
                File.Move(fileName, newFileName);
            }
    
        }