C++: Find and Highlight Text in Word

In a large document, it is inevitable to use the find function when you want to quickly locate specific key information. At the same time, highlighting them with a bright color is also an effective way to make them stand out to grab the reader's attention. In this article, you will learn how to programmatically find and highlight text in a Word document using Spire.Doc for C++.

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 and Highlight All Instances of a Specified Text in Word in C++

Spire.Doc for C++ offers the Document->FindAllString(LPCWSTR_S matchString, bool caseSensitive, bool wholeWord) method to find all instances of a specified text string, and then you can iterate through these instances to highlight them with a bright color. The following are the detailed steps.

  • Create a Document instance.
  • Load a Word document using Document->LoadFromFile() method.
  • Find all matching text in the document using Document->FindAllString() method.
  • Loop through all matching text in the document.
  • Get the text range of a specific matching text using TextSelection->GetAsOneRange() method, and then set its highlight color using TextRange->GetCharacterFormat()->SetHighlightColor() method.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace Spire::Common;

int main() {
	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\input1.docx";
	std::wstring outputFile = L"Output\\FindAndHighlightAll.docx";

	//Create a Document instance
	Document* document = new Document();

	//Load a Word document from disk
	document->LoadFromFile(inputFile.c_str());

	//Find all matching text in the document
	std::vector<TextSelection*> textSelections = document->FindAllString(L"Transcendentalism", false, true);

	//Loop through all matching text and set highlight color for them
	for (auto selection : textSelections)
	{
		selection->GetAsOneRange()->GetCharacterFormat()->SetHighlightColor(Color::GetYellow());
	}

	//Save the result document
	document->SaveToFile(outputFile.c_str(), FileFormat::Docx);
	document->Close();
	delete document;
}

C++: Find and Highlight Text in Word

Find and Highlight the First Instance of a Specified Text in Word in C++

You can also use the Document->FindString(LPCWSTR_S matchString, bool caseSensitive, bool wholeWord) method to find only the first instance of a specified text string and then set highlight color for it. The following are the detailed steps.

  • Create a Document instance.
  • Load a Word document using Document->LoadFromFile() method.
  • Find the first matching text using Document->FindString() method.
  • Get the text range of the first matching text using TextSelection->GetAsOneRange() method, and then set its highlight color using TextRange->GetCharacterFormat()->SetHighlightColor() method.
  • Save the result document using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace Spire::Common;

int main() {
	//Specify the input and output file paths
	std::wstring inputFile = L"Data\\input1.docx";
	std::wstring outputFile = L"Output\\FindAndHighlight.docx";

	//Create a Document instance
	Document* document = new Document();

	//Load a Word document from disk
	document->LoadFromFile(inputFile.c_str());

	//Find the first matching text
	TextSelection* textSelection = document->FindString(L"Transcendentalism", false, true);

	//Set highlight color for the text 
	textSelection->GetAsOneRange()->GetCharacterFormat()->SetHighlightColor(Color::GetYellow());

	//Save the result document
	document->SaveToFile(outputFile.c_str(), FileFormat::Docx);
	document->Close();
	delete document;
}

C++: Find and Highlight Text in Word

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.