Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/dd9e06fbbc3c60b11d00879b0b26ff62 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/dd9e06fbbc3c60b11d00879b0b26ff62 to your computer and use it in GitHub Desktop.
Convert DOCX to DOC | DOC to DOCX Programmatically using C++ | https://blog.aspose.com/2021/06/28/convert-docx-to-doc-or-doc-to-docx-using-cpp/
Read the complete article about converting DOCX to DOC or DOC to DOCX programmatically using C++ by visiting the following link.
https://blog.aspose.com/2021/06/28/convert-docx-to-doc-or-doc-to-docx-using-cpp/
// 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);
}
}
// 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);
// 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);
}
}
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment