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 Apr 28, 2025 8:15 am

Im using the sample code to replace text in content controls in a word file. When using TrackChanges I can see that the text was actually replaced at some point but it gets immediately reversed back to the text it was supposed to replace.

Code: Select all
        private async Task ReplaceContentControlText(Spire.Doc.Document document, string tag, string textToReplace, string newText)
        {
            Body body = document.Sections[0].Body;

            // Create lists for paragraphs and tables
            List<Paragraph> paragraphs = new List<Paragraph>();
            for (int i = 0; i < body.ChildObjects.Count; i++)
            {
                // Get the document object
                DocumentObject documentObject = body.ChildObjects[i];

                // If it is a StructureDocumentTag object
                if (documentObject.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                {
                    StructureDocumentTag structureDocumentTag = (StructureDocumentTag)documentObject;
                    if (structureDocumentTag.SDTProperties.Tag == tag || structureDocumentTag.SDTProperties.Alias == tag)
                    {
                        for (int j = 0; j < structureDocumentTag.ChildObjects.Count; j++)
                        {
                            // If it is a paragraph object
                            if (structureDocumentTag.ChildObjects[j].DocumentObjectType == DocumentObjectType.Paragraph)
                            {
                                Paragraph paragraph = (Paragraph)structureDocumentTag.ChildObjects[j];
                                //paragraphs.Add(paragraph);
                                paragraph.Replace(textToReplace, newText ?? string.Empty, true, true);
                                //paragraph.Text = newText;
                                document.AcceptChanges();
                            }

                        }
                    }
                }
            }


            // Modify the text content of the first paragraph
            if (paragraphs.Count > 0)
            {
                Console.WriteLine(paragraphs[0].Text);
                paragraphs[0].Replace(textToReplace, newText ?? string.Empty, true, true);
                //paragraphs[0].Text = newText;
                Console.WriteLine(paragraphs[0].Text);
            }

        }

Jasper_wit
 
Posts: 1
Joined: Mon Apr 28, 2025 8:08 am

Mon Apr 28, 2025 10:13 am

Hello,

Thanks for your inquiry.
I simulated a word document to verify your case, but failed to reproduce the issue you mentioned. To help us investigate further, please provide us with the following information. You can upload them to the attachment or send them to this email address [email protected]. Thanks in advance.
1. The version of Spire.Doc you are using.
2. Your test document and complete test code. If possible, please share the complete test code. The "tag" parameter provided to the code is unknown.
3. Some screenshots describing your current issue.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc