Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Feb 18, 2019 8:18 pm

Hello,

I am trying to copy paragraphs that contain the words "shall or "will into another file. Can someone please give me some direction on how to do this. I am not very familiar with C# but love your product, it makes things so simple. I appreciate your time.

I found this, but am unsure how to implement it with what I need.

Spire.Doc.Document doc = new Spire.Doc.Document();
doc.LoadFromFile(@"..\..\test.docx");
Spire.Doc.Section section = doc.Sections[0];
//”Burning.liu” as a “key text”
TextSelection selection = doc.FindString("The contractor shall", true, true);
TextRange range = selection.GetAsOneRange();
Paragraph paragraph = range.OwnerParagraph;
Body body = paragraph.OwnerTextBody;
int index = body.ChildObjects.IndexOf(paragraph);

Thanks in advance!

Ed

edward.bonner@jtsi.net
 
Posts: 2
Joined: Tue Jan 22, 2019 3:24 pm

Tue Feb 19, 2019 6:50 am

Hello,

Thanks for your inquiry.
Please refer to the following sample code to achieve your demand. If there is any question, please provide your input Word files to help us further investigate it. You could send them to us via email (support@e-iceblue.com).
Code: Select all
 //the file for copying
 Spire.Doc.Document otherDoc = new Spire.Doc.Document();
 otherDoc.LoadFromFile(@"other.docx");
 //the file for finding
 Spire.Doc.Document doc = new Spire.Doc.Document();
 doc.LoadFromFile(@"test.docx");
 Section section = doc.Sections[0];
 //"shall or" as a key
 TextSelection[] selection = doc.FindAllString("shall or", true, true);
 foreach (TextSelection slec in selection)
 {
     TextRange range = slec.GetAsOneRange();
     Paragraph paragraph = range.OwnerParagraph;
     //copy the found paragraph to other file
     otherDoc.LastSection.Body.ChildObjects.Add(paragraph.Clone());
 }
 otherDoc.SaveToFile("result.docx");

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Tue Feb 19, 2019 1:42 pm

Hello Lisa and thank you very much for the reply. When I attempt to use the code I get the following error:

Error 7 The type or namespace name 'TextRange' could not be found (are you missing a using directive or an assembly reference?)

Do you have any suggestions. I tried to add all the Spire Assemblies but it did not solve the problem. The rest of the code works perfectly up to that point.

NEVERMIND I FOUND THE SOLUTION! YOU GUYS ARE AWESOME!!!!!!!

Thanks
Ed

edward.bonner@jtsi.net
 
Posts: 2
Joined: Tue Jan 22, 2019 3:24 pm

Wed Feb 20, 2019 1:23 am

Hello,

Thanks for your quick response.
If you have other questions, please do not hesitate to contact us. Wish you all the best!

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc