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.

Tue Aug 24, 2021 8:05 am

Hi Spire-Team,

suppose you have a Word document that contains three elements [[START]], [[END]] and [[TARGET]] somewhere inside the document.
Al three elements are simple text (they are not Mail merge fields or anything fancy like this).

----------------------------------------------------------------------------------------------------------
[[START]]
Text, Tables, Images (and all their styles)
[[END]]

More text etc.

[[TARGET]]

More text etc.
----------------------------------------------------------------------------------------------------------

I can't tell what is between [[START]] and [[END]]. But it's probably text, tables and sometimes images.

My goal is: I would like to create an exact copy of *whatever* is between [[START]] and [[END]] and then finally replace [[TARGET] with this copy.
I have tried so many things but always something is missing.

Do you see a way to achieve my goal? That would be awesome.
Thank you guys!

spire@softwarea.de
 
Posts: 7
Joined: Thu May 03, 2018 8:56 pm

Tue Aug 24, 2021 11:10 am

Hello,

Thanks for your inquiry.
Below is sample code for your reference. Please give it a try. If there are still any questions when testing, in order to help us further analyze this issue, please provide your input Word file. Thanks in advance.

Code: Select all
    Document doc = new Document();
    doc.LoadFromFile("Test.doc");
    List<DocumentObject> objList = new List<DocumentObject>();
    TextSelection startSelection = doc.FindString("[[START]]",false,true);
    TextSelection endSelection = doc.FindString("[[END]]", false, true);
    TextSelection targetSelection = doc.FindString("[[TARGET]]", false, true);

    Paragraph targetPara = targetSelection.GetAsOneRange().OwnerParagraph;
    Paragraph startPara = startSelection.GetAsOneRange().OwnerParagraph;
    Paragraph endPara = endSelection.GetAsOneRange().OwnerParagraph;
    int sectionIndex=-1;
    foreach (Section section in doc.Sections)
    {
        foreach (DocumentObject obj in section.Body.ChildObjects)
        {
            objList.Add(obj);
            if(obj is Paragraph)
            {
                Paragraph paragraph = obj as Paragraph;
                if (paragraph.Equals(targetPara))
                {
                    sectionIndex = doc.Sections.IndexOf(section);
                }
            }
        }
    }
    int startIndex = objList.IndexOf(startPara);
    int endIndex = objList.IndexOf(endPara);
    Section targetSection = doc.Sections[sectionIndex];
    int insertIndex = targetSection.Body.ChildObjects.IndexOf(targetPara);
    targetSection.Paragraphs.Remove(targetPara);
    for(int i=endIndex-1;i>startIndex;i--)
    {
        targetSection.Body.ChildObjects.Insert(insertIndex, objList[i].Clone());
    }
    doc.SaveToFile("output.docx",FileFormat.Docx);
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Tue Aug 24, 2021 8:49 pm

Hello Andy, thank you SO much. Your code works like a charm!

spire@softwarea.de
 
Posts: 7
Joined: Thu May 03, 2018 8:56 pm

Wed Aug 25, 2021 11:16 am

Hi,

Thanks for your reply.
You are welcome! If you encounter any issues related to our product in the future, just feel free to contact us.
Have a nice day!
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.Doc