omar wasfi
omar wasfi

Reputation: 567

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 ) ?

Upvotes: 2

Views: 424

Answers (1)

omar wasfi
omar wasfi

Reputation: 567

    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);
        }

    }

Upvotes: 1

Related Questions