This topic shows, how to find and replace text in Word document in C++ using a simplified code example. By using C++ replace text in Word document is easily performed in few lines of easy code with no external dependence on MS Word or Interop library. You can use the application in any of C++ supported application running in Windows, macOS or Linux platforms.
Steps to Replace Text in Word document in C++
- Configure the latest version of Aspose.Words.CPP from NuGet Package Manager
- Include Aspose::Words namespace and add required Header files
- Initialize Document class object to load DOCX for replacing text
- Create FindReplaceOptions class object to enable different properties
- Perform text replacement for selected search and replace string
- Save the Word document with replaced text on disk
In aforementioned steps, we will first be loading the source Word document from disk and then specify different properties to set the criteria for search and replace the text, including replace direction, match case and finding whole words. Finally, we will replace text in word document using C++ and save the modified DOCX on disk.
Code to Replace Text in Word document using C++
In this example, using C++ Word document replace text is easily performed using simple API calls. By using an instance of Document class, the input Word document will be loaded using API. Then in subsequent steps, we will make use of FindReplaceOptions class to set the search and replace criteria by setting different properties. Finally, we will save the modified DOCX on disk or in MemoryStream as an output.
In this article, we have explored about how to find and replace text in Word document using C++. However, if you want to learn about exporting Word file to PDF, refer to the article on how to convert Word to PDF using C++.