This project has moved and is read-only. For the latest updates, please go here.

ReplaceText issue

Dec 14, 2015 at 12:33 PM

I found a possible bug while doing a ReplaceText operation. The weird thing is that DocX.Text contains the new/expected text while the output document still shows the initial values!
It looks like it is related to the input document because it works fine for other document.
I can provide you the word doc.

Is this a know issue?

Thank you,
Apr 11, 2016 at 4:50 PM
Hey alexBa,
I have the same issue.  If I found a work-around to this problem, I'll let you know.
Thank you,

Apr 11, 2016 at 4:52 PM
It would be easier if you provide source codes what are you using. Expecting help with 0 information won't give you much.
Apr 11, 2016 at 5:09 PM
Hey MadBoy,
No worries, thanks for the quick response.  
Issue -
Attempting to replace a block of text like: <textblock>All the wording inside here.</textblock>.
        Public Sub RemoveTextBlock(ByRef document As DocX, ByVal BeginTagText As String, ByVal EndTagText As String)

            Dim lstTagStart As List(Of Integer) = document.FindAll(BeginTagText)
            Dim lstTagEnd As List(Of Integer) = document.FindAll(EndTagText)
            Dim strRemoveMe As String = ""
            Dim x As Integer = 0


            If lstTagStart.Count = lstTagEnd.Count Then

                For x = 0 To lstTagStart.Count - 1

                    'find the text block
                    strRemoveMe = document.Text.Substring(lstTagStart(x), lstTagEnd(x) - lstTagStart(x) + EndTagText.Length)

                    'replace the text block with ""  
                    document.ReplaceText(strRemoveMe, "", False, RegexOptions.IgnorePatternWhitespace)

            End If


        End Sub
strRemoveMe = "0. <contact>You can file this information with your representative. </cont" instead of "<contact>You can file this information with your representative. </contact>".

When other text throughout the document is replaced, it appears to change the results of this expression. I just started looking into the source code about five minutes ago, so I'll reply if I find a resolution. Any assistance would be appreciated.


Apr 11, 2016 at 5:11 PM
I assume you're using latest version?
Apr 11, 2016 at 5:18 PM
Latest version available in NuGet (v1.0.0.19). If this is resolved in, I'll reference the library directly.
Apr 11, 2016 at 5:20 PM
I would recommend always testing with libraries from here or github. I don't manage NuGet site.
Apr 11, 2016 at 5:22 PM
Ok, thanks MadBoy. I'll download the files, test and let you know.
Apr 11, 2016 at 5:33 PM
Quick update: Receiving the same results from
Apr 11, 2016 at 6:03 PM
I've tested this on simple scenario:
    static void ReplaceTextExamplel()
        const string a = "business";
        const string b = "pear";
        // Load the document.
        using (DocX document = DocX.Load(@"..\..\docs\Template.docx"))
            // Replace text in this document.
            document.ReplaceText(a, b,false,RegexOptions.IgnoreCase);
            // Save changes made to this document.
        } // Release this document from memory.
        Console.WriteLine("\tCreated: None\n");
Replaces business with pear. Works on my template.
Apr 11, 2016 at 6:06 PM
As for the 1st topic entry I would assume @alexBa problem is in .Save command not being executed maybe. Hard to say as there is no source code.
Apr 11, 2016 at 7:18 PM
Thanks madboy,
 I'll troubleshooting the issue with my scenario on the latest source.