In Aspose.Words, we normally use the Document constructor of Aspose.Words API to load a document in DOCM format and the Document.Save method to save the document to DOCX.
The following code example shows how to convert DOCM to DOCX:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Docm to Docx.docm");
doc.Save(ArtifactsDir + "Docm to Docx - Aspose.Words.docx");
You can also do the same using the Open XML SDK. At the same time, note that it looks somewhat more complicated and more cumbersome.
The following code example modifies the specified document by verifying that the document contains a vbaProject part and removing that part. After the code removes the part, it changes the document type internally and renames the document so that it uses .docx extension.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
string docmFilePath = MyDir + "Docm to Docx.docm";
string docxFilePath = ArtifactsDir + "Docm to Docx - OpenXML.docx";
using (WordprocessingDocument docm = WordprocessingDocument.Open(docmFilePath, false))
{
// Create a copy of the .docm file as .docx.
File.Copy(docmFilePath, docxFilePath, true);
// Open the new .docx file and remove the macros.
using (WordprocessingDocument docx = WordprocessingDocument.Open(docxFilePath, true))
{
// Remove the VBA project part (macros).
VbaProjectPart vbaPart = docx.MainDocumentPart.VbaProjectPart;
if (vbaPart != null)
docx.MainDocumentPart.DeletePart(vbaPart);
// Change the document type to .docx (no macros).
docx.ChangeDocumentType(WordprocessingDocumentType.Document);
}
}