The find and replace feature in Microsoft Word is an essential tool when editing documents. It enables you to quickly find a specific word or phrase in a Word document and lets you replace all instances of it at once. This is especially helpful in situations where you need to update information or correct misspelled words in large Word documents. In this article, you will learn how to find and replace text in Word documents in C++ using Spire.Doc for C++.
- Find Text and Replace All Its Instances with New Text
- Find Text and Replace Its First Instance with New Text
- Find and Replace Text Using a Regular Expression
- Find and Replace Text with an Image
Install Spire.Doc for C++
There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.
Integrate Spire.Doc for C++ in a C++ Application
Find Text and Replace All Its Instances with New Text
You can find a text and replace all its instances with another text easily using the Document->Replace() method. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document->LoadFromFile() method.
- Find a specific text and replace all its instances with another text using Document->Replace() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h" using namespace Spire::Doc; using namespace std; int main() { //Initialize an instance of the Document class Document* document = new Document(); //Load a Word document document->LoadFromFile(L"Input.docx"); //Find a specific text and replace all its instances with another text document->Replace(L"Spire.Doc", L"Eiceblue", false, true); //Save the result document document->SaveToFile(L"ReplaceAllInstances.docx", FileFormat::Docx2013); document->Close(); delete document; }
Find Text and Replace Its First Instance with New Text
Spire.Doc for C++ provides the Document->SetReplaceFirst() method which enables you to change the replacement mode from replacing all instances to replacing the first instance. The following steps explain how to find a text and replace its first instance:
- Initialize an instance of the Document class.
- Load a Word document using Document->LoadFromFile() method.
- Change the replacement mode to replace the first instance using Document->SetReplaceFirst(true) method.
- Replace the first instance of a text with another text using Document->Replace() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h" using namespace Spire::Doc; using namespace std; int main() { //Initialize an instance of the Document class Document* document = new Document(); //Load a Word document document->LoadFromFile(L"Input.docx"); //Change the replacement mode to replace the first match document->SetReplaceFirst(true); //Replace the first instance of a text with another text document->Replace(L"Spire.Doc", L"Eiceblue", false, true); //Save the result document document->SaveToFile(L"ReplaceFirstInstance.docx", FileFormat::Docx2013); document->Close(); delete document; }
Find and Replace Text Using a Regular Expression
You can replace a text matching a regular expression with new text using the Document->Replace() method and passing a Regex instance and the new text to the method as parameters. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document->LoadFromFile() method.
- Initialize an instance of the Regex class to create a regular expression.
- Find the text matching the regex and replace it with another text using Document->Replace() method.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h" using namespace Spire::Doc; using namespace std; int main() { //Initialize an instance of the Document class Document* doc = new Document(); //Load a Word document doc->LoadFromFile(L"Input1.docx"); //Create a regex to match the text that starts with # Regex* regex = new Regex(L"\\#\\w+\\b"); //Find the text matching the regex and replace it with another text doc->Replace(regex, L"Monitor"); //Save the result document doc->SaveToFile(L"ReplaceWithRegex.docx", FileFormat::Docx2013); doc->Close(); delete doc; }
Find and Replace Text with an Image
Spire.Doc for C++ doesn't offer a direct method to replace text with image, but you can achieve this by inserting the image at the position of the text and then removing the text from the document. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document->LoadFromFile() method.
- Find a specific text using Document->FindAllString() method and put the found text into a vector.
- Iterate through the found text in the vector.
- Initialize an instance of DocPicture class and load an image using DocPicture->LoadImageSpire() method.
- Get the found text as a single text range and then get the index of the text range in its owner paragraph.
- Insert an image at the position of the text range and then remove the text range from the document.
- Save the result document using Document->SaveToFile() method.
- C++
#include "Spire.Doc.o.h" using namespace Spire::Doc; using namespace std; int main() { //Initialize an instance of the Document class Document* doc = new Document(); //Load a Word document doc->LoadFromFile(L"Input.docx"); //Find a text in the document and put the found results into a vector vector<TextSelection*> selections = doc->FindAllString(L"Spire.Doc", true, true); int index = 0; TextRange* range = nullptr; //Iterate through the found text in the vector for (auto selection : selections) { //Load an image DocPicture* pic = new DocPicture(doc); pic->LoadImageSpire(L"img.jpg"); //Get the found text as a single text range range = selection->GetAsOneRange(); //Get the index of the text range in its owner paragraph index = range->GetOwnerParagraph()->GetChildObjects()->IndexOf(range); //Insert an image at the index range->GetOwnerParagraph()->GetChildObjects()->Insert(index, pic); //Remove the text range range->GetOwnerParagraph()->GetChildObjects()->Remove(range); } //Save the result document doc->SaveToFile(L"ReplaceWithImage.docx", FileFormat::Docx2013); doc->Close(); delete doc; }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.