Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active January 12, 2021 16:50
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/1e31a5c8733210d428000bc330d6199f to your computer and use it in GitHub Desktop.
Save aspose-com-gists/1e31a5c8733210d428000bc330d6199f to your computer and use it in GitHub Desktop.
Find and Replace Text in Word C++
// Load MS Word document
System::SharedPtr<Document> doc = System::MakeObject<Document>(u"Document.doc");
// Create find and replace options
System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>();
// Replace 'e' in document ignoring text inside field
options->set_IgnoreFields(true);
doc->get_Range()->Replace(System::MakeObject<Regex>(u"e"), u"*", options);
// Replace 'e' in document ignoring deleted text
options->set_IgnoreDeleted(true);
doc->get_Range()->Replace(System::MakeObject<Regex>(u"e"), u"*", options);
// Replace 'e' in document ignoring inserted text
options->set_IgnoreInserted(true);
doc->get_Range()->Replace(System::MakeObject<Regex>(u"e"), u"*", options);
// Save the updated document
doc->Save(u"updated.doc");
// Load MS Word document
System::SharedPtr<Document> doc = System::MakeObject<Document>(u"Document.doc");
// Create find and replace options
System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>();
// Find and replace the text
doc->get_Range()->Replace(u"This is Line 1&pThis is Line 2", u"This is replaced line", options);
doc->get_Range()->Replace(u"This is Line 1&mThis is Line 2", u"Page break is replaced with new text.", options);
// Save the updated document
doc->Save(u"updated.doc");
// Load MS Word document
System::SharedPtr<Document> doc = System::MakeObject<Document>(u"Document.doc");
// Create find and replace options
System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>();
// Find and replace the text
doc->get_Range()->Replace(System::MakeObject<System::Text::RegularExpressions::Regex>(u"[s|m]ad"), u"bad", options);
// Save the updated document
doc->Save(u"updated.doc");
// Load MS Word document
System::SharedPtr<Document> doc = System::MakeObject<Document>(u"Document.doc");
// Find and replace the text
doc->get_Range()->Replace(u"sad", u"bad", System::MakeObject<FindReplaceOptions>(FindReplaceDirection::Forward));
// Save the updated document
doc->Save(u"updated.doc");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment