Find and Replace Text in Word Documents using C++

find and replace text in Word C++

Find and replace is a useful feature provided by MS Word that lets you update all occurrences of a particular text at once. Thus, you do not have to locate and replace the text in the whole document manually. In this article, you will learn how to find and replace text in Word documents from within C++ applications. This could be useful when you need to replace a particular text in a number of documents in one go.

C++ API to Find and Replace Text

Aspose.Words for C++ is a C++ library for creating new and manipulating existing Word documents. The API supports a wide range of basic as well as advanced Word automation features. You can download the API package or install it using NuGet.

Install-Package Aspose.Words.Cpp

Find and Replace Text in Word Documents using C++

The following are the steps to find and replace text in Word documents using Aspose.Words for C++.

  • Load the Word document using Document class.
  • Replace the desired word using Document->get_Range()->Replace(u”sad”, u”bad”, System::MakeObject<FindReplaceOptions>(FindReplaceDirection::Forward)) method.
  • Save the updated Word document using Document->Save(String) method.

The following code sample shows how to find and replace the word “sad” with “bad” in a Word document using C++.

Find and Replace Text using Regular Expressions

You can also define the regular expressions in order to find and replace the words that follow a particular pattern. For example, you can replace the words “sad” and “mad” with the word “bad”. The following are the steps to find and replace words matching a regular expression in a Word document.

The following code sample shows how to find and replace text using regular expression in C++.

Find and Replace Text using Meta-Characters

In certain cases, the text you want to replace could include the breaks i.e. line break, paragraph break, section break, etc. In order to deal with such scenarios, Aspose.Words for C++ supports the following meta characters in the search and replacement strings.

  • &p for a paragraph break
  • &b for section break
  • &m for page break
  • &l for a manual line break

The following code sample shows how to find and replace text using meta-characters in a Word document.

Ignore Text during Find and Replace Operation

Aspose.Words for C++ also allows you to ignore the text within fields and the revisions during the find and replace operation. The FindReplaceOptions class lets you specify the options to add this customization. The FindReplaceOptions class provides the following methods to ignore text in different scenarios:

The following code sample shows how to ignore text in each of the above-mentioned scenarios.

Get a Free License

You can get a free temporary license in order to try the API without evaluation limitations.

Conclusion

In this article, you have seen how to find and replace text in Word documents using C++. The step by step guide and code samples have shown how to customize the find and replace feature in different scenarios. You can explore more about the C++ Word automation API using documentation.

See Also