Replace Special Character


#1

Hi there,

I'm experiencing some troubles with Aspose.Words.
I'm tryin to replace spacers (###somtext###) I've placed into the Word Document by using:
objDoc.Range.Replace
Now I've tried this with a larger text containing special characters and I got the message: The replace string cannot contain special or break characters.

I already looked through the documentation and it says it's not possible to perform a replace with text containing the special characters. I've also looked through the forum if there is a workaround but couldn't find anything.

Can I get aroung this in some way?

Thx in Advance

Steve


#2

I am afraid there is no workaround for this. Putting special characters in replacement string will lead to implicit change of document structure and formatting with unpredictable results and probable invalidation of the document. The same will happen in html or xml if you will insert some random portions of text there containing tag symbols (<,>). That's why we prohibit it.

Best regards,


#3

What about Mailmerge oder InsertHTML?

Isn't there something that could be used to insert text that contains line breaks?

Thx in Advance

Steve


#4

Yes, you are right. This can be a nice workaround. Here is the code illustrating this approach:

private void ReplaceWithInsertHtml(Document doc)

{

doc.Range.Replace(new Regex("Hello"), new ReplaceEvaluator(ReplaceEvaluator1), false);

}

private ReplaceAction ReplaceEvaluator1(object sender, ReplaceEvaluatorArgs e)

{

DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);

builder.MoveTo(e.MatchNode);

builder.InsertHtml("Some Text

More text

");

e.Replacement = "";

return ReplaceAction.Replace;

}

Best regards,