Free Support Forum - aspose.com

Problem while replacing string

Hi,

I am facing some problem while replacing string in a document using Range object.

The sample code is

Range oRange = m_oOutDoc.Range;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

int nReplace = oRange.Replace(“, “\r\r\rNovember 28, 007\r\r\rAB Road ", false, false);

The last statement actually throwing exception

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

Teh placeholder exists in a document and I'm reading the address from another document and repalcing the placeholder. The address read from the other document contains newline characters.

Thank you

Hi

Thank you for your interest in Aspose.Words. Replacement shouldn’t contain special characters. But you can achieve this using ReplaceEvaluator. For example see the following code.

public void TestReplace_106412()

{

Document doc = new Document("in.doc");

Regex regex = new Regex("");

doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceAction_106412), true);

doc.Save("out.doc");

}

ReplaceAction ReplaceAction_106412(object sender, ReplaceEvaluatorArgs e)

{

Run run = (Run)e.MatchNode;

run.Text = string.Empty;

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

builder.MoveTo(run);

builder.Write("\r\r\rNovember 28, 007\r\r\rAB Road");

return ReplaceAction.Skip;

}

I hope that this will help you.

Best regards.