Free Support Forum - aspose.com

Replace string with new containing newlines?

I am trying to replace various sections of a word document automatically with new values that contain newlines. However, every call to Range.Replace(…) yields me:


System.ArgumentException: The replace string cannot contain special or break characters.
at ֍.䀾.set_䁀(String Ӷ)
at Aspose.Words.Range.Replace(String oldValue, String newValue, Boolean isMatchCase, Boolean isMatchWholeWord)
at …

Am I missing something or is this absolutely not possible with this function? Would it be possible with a mail merge?

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Please try using line break instead of paragraph break. Please see the following code:

Document doc = new Document(@"Test001\in.doc");

doc.Range.Replace("[PlaceHolder]", "This is the first line\vThis is the second line", false, false);

doc.Save(@"Test001\out.doc");

If you need paragraph breaks in your output document, you should use mergefields instead of text placeholders and use Mail Merge feature to fill the document with data. Here is simple code:

Document doc = new Document(@"Test001\in.doc");

doc.MailMerge.Execute(new string[] { "MyField" }, new object[] { "This is the first line\nThis is the second line" });

doc.Save(@"Test001\out.doc");

Hope this helps.

Best regards.