Convert DOCX to DOC or DOC to DOCX using C++

Microsoft Word documents are available in two formats, DOC and DOCX. DOC is an older format, and DOCX is its successor. You can convert DOCX files to DOC format and vice-versa. In this article, you will learn how to convert a DOCX file to DOC format and a DOC file to DOCX format. You will also see how to perform these conversions in bulk. To explore these scenarios in detail, please see the following sections:

C++ API for Converting DOCX to DOC and DOC to DOCX Format

Aspose.Words for C++ is a native C++ library that allows you to create, read, and modify Microsoft Word documents. Furthermore, it also supports converting DOCX files to DOC format and DOC files to DOCX format. You can either install the API through NuGet or download it directly from the Downloads section.

PM> Install-Package Aspose.Words.Cpp

Converting DOCX File to DOC Format using C++

You can convert a DOCX file to DOC format by following the steps given below.

The following sample code shows how to convert a DOCX file to a DOC file using C++.

// Create an instance of the LoadOptions class
auto loadOptions = System::MakeObject<LoadOptions>();
// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Docx);
// Load source DOCX file
System::SharedPtr<Document> doc = System::MakeObject<Document>(u"SourceDirectory\\Word\\Sample 4.docx", loadOptions);
// Save the DOC file
doc->Save(u"OutputDirectory\\output.doc", SaveFormat::Doc);

Batch Convert DOCX Files to DOC Format using C++

If you have hundreds of DOCX files that you need to convert to DOC format, you can easily convert them by executing the program once. In this example, we will use the boost library to iterate through the files in the directory. The following are the steps to batch convert DOCX files to DOC format.

The following sample code demonstrates how to convert DOCX files to DOC format in bulk using C++.

// Iterate through the files in the directory
for (directory_entry& file : directory_iterator("SourceDirectory\\Word"))
{
// Check file extension
if (file.path().extension().string() == ".docx")
{
// Create an instance of the LoadOptions class
auto loadOptions = System::MakeObject<LoadOptions>();
// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Docx);
// Load the DOCX file
System::SharedPtr<Document> doc = System::MakeObject<Document>((System::String)file.path().string(), loadOptions);
// Change the file extension
System::String fileName = (System::String)file.path().filename().string();
fileName = fileName.Replace(u".docx", u".doc");
// Save the DOC file
doc->Save(System::String::Concat(u"OutputDirectory\\", fileName), SaveFormat::Doc);
}
}

Convert DOC File to DOCX Format using C++

The following are the steps to convert a DOC file to a DOCX file.

The following sample code shows how to convert a DOC file to a DOCX file using C++.

// Create an instance of the LoadOptions class
auto loadOptions = System::MakeObject<LoadOptions>();
// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Doc);
// Load source DOC file
System::SharedPtr<Document> doc = System::MakeObject<Document>(u"SourceDirectory\\Word\\Sample 1.doc", loadOptions);
// Save the DOCX file
doc->Save(u"OutputDirectory\\output.docx", SaveFormat::Docx);

Batch Convert DOC Files to DOCX Format using C++

In order to convert DOC files to DOCX format in bulk, use the steps given below.

The following sample code demonstrates how to convert DOC files to DOCX format in bulk using C++.

// Iterate through the files in the directory
for (directory_entry& file : directory_iterator("SourceDirectory\\Word"))
{
// Check file extension
if (file.path().extension().string() == ".doc")
{
// Create an instance of the LoadOptions class
auto loadOptions = System::MakeObject<LoadOptions>();
// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Doc);
// Load the DOC file
System::SharedPtr<Document> doc = System::MakeObject<Document>((System::String)file.path().string(), loadOptions);
// Change the file extension
System::String fileName = (System::String)file.path().filename().string();
fileName = fileName.Replace(u".doc", u".docx");
// Save the DOCX file
doc->Save(System::String::Concat(u"OutputDirectory\\", fileName), SaveFormat::Docx);
}
}

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to convert a DOCX file to DOC format and a DOC file to DOCX format using C++. Furthermore, you have seen how to perform these conversions in bulk using the boost library. Aspose.Words for C++ provides a bunch of additional features for automating your Word-related tasks. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.

See Also