Last active
January 12, 2021 16:50
-
-
Save aspose-com-gists/1e31a5c8733210d428000bc330d6199f to your computer and use it in GitHub Desktop.
Find and Replace Text in Word C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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