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:
Document doc = new Document("SourceDocument.docm");
doc.Save("ResultDocument.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.
Following are the namespaces we need to add:
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using NUnit.Framework;
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.
public void ConvertFromDocmToDocxFeature()
{
bool fileChanged = false;
using (WordprocessingDocument document =
WordprocessingDocument.Open(MyDir + "Convert from docm to docx.docm", true))
{
var docPart = document.MainDocumentPart;
// Look for the vbaProject part. If it is there, delete it.
var vbaPart = docPart.VbaProjectPart;
if (vbaPart != null)
{
// Delete the vbaProject part and then save the document.
docPart.DeletePart(vbaPart);
docPart.Document.Save();
// Change the document type to not macro-enabled.
document.ChangeDocumentType(
WordprocessingDocumentType.Document);
fileChanged = true;
}
}
// If anything goes wrong in this file handling,
// the code will raise an exception back to the caller.
if (fileChanged)
{
if (File.Exists(ArtifactsDir + "Convert from docm to docx - OpenXML.docm"))
File.Delete(ArtifactsDir + "Convert from docm to docx - OpenXML.docm");
File.Move(MyDir + "Convert from docm to docx.docm",
ArtifactsDir + "Convert from docm to docx - OpenXML.docm");
}
}