�The replace string cannot contain special or break characters.�

Hello!
I have some problem with replacing special characther in a Word file(.doc).
In the word file there are 2 field to replace:
- Subject
- Memo

I Have some problem with Memo because the string to replace in it has some vbCrLf.
The string to StringReplace is:
“Line 1
Line 2
Line 3
Line 4”

This is my vb code

Dim StringReplace as string

StringReplace = StringReplace.Replace(vbCrLf, Aspose.Words.ControlChar.LineBreakChar)
doc.Range.Replace("«Memo»", StringReplace, False, False)

Works Great!

I Need to replace vbCrLf with ParagraphBreak and not with LineBreak.
If I write:

StringReplace = StringReplace.Replace(vbCrLf, Aspose.Words.ControlChar.ParagraphBreakChar)
doc.Range.Replace("«Memo»", StringReplace, False, False)

Error: “The replace string cannot contain special or break characters.”

I have word file with text alignment JUSTIFY.
If I replace with LineBreak o Paragraph is not the same.

thanks!

I Tried with Mail Merge but It’s not for my purpose.
In the attached Word File I inserted Field Manually anche not with this procedure https://docs.aspose.com/words/net/mail-merge-template/

I have a lot of documents with field inserted “manually” and I Can’t change all of them.

How Can I Solve?

thanks!

Hi Matteo,

Thanks for your inquiry.

While using the latest version of Aspose.Words i.e. 14.5.0, I managed to reproduce this issue on my side. I have logged this issue in our bug tracking system. The ID of this issue is WORDSNET-10234. Your thread has also been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best regards,

Hi Matteo,

Thanks for being patient. Please use the following code to resolve this issue:

Dim doc As New Document(MyDir & "example.doc")
doc.Range.Replace(New Regex("«Memo»", RegexOptions.None), New MyReplaceEvaluator(), False)
doc.Save(MyDir & "out.doc")
Private Class MyReplaceEvaluator
    Implements IReplacingCallback
    Private Function IReplacingCallback_Replacing(ByVal e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing
        Dim StringReplace As String = File.ReadAllText(MyDir & "in.txt")
        StringReplace = StringReplace.Replace(vbCrLf, Aspose.Words.ControlChar.ParagraphBreakChar)
        Dim builder As New DocumentBuilder(e.MatchNode.Document)
        builder.MoveTo(e.MatchNode)
        builder.Write(StringReplace)
        Return ReplaceAction.Replace
    End Function
End Class

Please see attached files. I hope, this helps.

Best regards,