Hi ALL :
I am working with Aspose Words . It worked great till now .Happy with the progress .
I am Stuck with find and Replace Functioanlity . Regular find and replace works great .
My Requirement : Find a sentence as whole ,which has merge field in it . And Replace that whole sentence with another sentence which also contains merge field.
Example :
Sentence 1: “blah blah blah <> blah blah”
Sentence 2 : "blah blah blah <> somthing else with next text blah blah"
Now i want to replace the Sentence 1 in my whole document ,with sentence 2
Hope fully its somthing easy to accomplish with aspose
Regards,
Jaswanth
http://www.aspose.com/docs/display/wordsnet/Moving+the+Cursor
http://www.aspose.com/docs/display/wordsnet/Inserting+Document+Elements
Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToMergeField("mergeField", true, false);
//Paragraph that contain the merge field.
Paragraph para = (Paragraph)builder.CurrentNode.ParentNode;
builder.MoveTo(para);
builder.InsertBreak(BreakType.ParagraphBreak);
builder.Write("new paragraph ");
builder.InsertField(@"MERGEFIELD MyFieldName \* MERGEFORMAT");
builder.Write(" somthing else with next text blah blah");
para.Remove();
doc.Save(MyDir + "Out.docx");
You can also achieve your requirement by implementing IReplacingCallback interface.
Please use the same approach shared at following documentation link to
find text and insert new paragraph.
Hi ,
Thank You for the response .
The Issue is . Sentence 1 is a small part of a paragraph . The Other text in the paragraph i wanted to maintain that text . Its just that ,i wanted to replace the whole Sentence 1 with Sentence 2 in the paragraph and maintain the rest of the paragraph .
Dont want to remove the entire paragraph.
-Jaswantth
Hi Jaswantth,
Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.
- Please attach your input Word document.
- Please
attach your target Word document showing the desired behavior. You can
use Microsoft Word to create your target Word document. I will
investigate as to how you are expecting your final document be generated
like.
us we will then provide you more information about your query along with code.
Hi
The input is as follow in the document .
INPUT :
This is a wonderful paragraph with lots of twists and tales. Please remove this sentence and replace <> with another sentence.Hello this is a wonder morning with lots monday blues.
OUTPUT :
This is a wonderful paragraph with lots of twists and tales. This has been such a wonderful challenge tag along so many <> accross all the platforms.Hello this is a wonder morning with lots monday blues.
-------------------------------------------------------------------------------------------------------------------
I hope this makes sense . Please let me know if you still need the input and output documents.
Now the bottom line is , I wanted to replace the whole “Please remove this sentence and replace <> with another sentence.” in my entire document where ever the sentence matches with "This has been such a wonderful challenge tag along so many <> accross all the platforms."
Thanks,
Jaswanth
Hi Jaswantth,
Thanks for sharing the detail. Please use the following code example to achieve your requirements. I have attached the input and output documents with this post for your kind reference. Hope this helps you.
Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
doc.Range.Replace(@"Please remove this sentence and replace", "This has been such a wonderful challenge tag along so many", false, false);
doc.Range.Replace(@"with another sentence", "accross all the platforms", false, false);
//Following two lines of code replace only first occurance of mail merge field with new mail merge field.
builder.MoveToMergeField("MergeField");
builder.InsertField(@"MERGEFIELD MergeField2 \* MERGEFORMAT");
doc.Save(MyDir + "Out.docx");