Hi,
I am getting problem while replacing string in a range. This string may contain paragraph character.
while inserting i am specifying ^p as paragraph charecter.
i am putting my code below.
public void InsertReplace()
{
m_oRange = m_oOutDoc.Range; // m_oOutDoc is document object
Regex regexFindPlaceholder = new Regex(@"<%(?\S+)%");
m_oRange.Replace(regexFindPlaceholder, new ReplaceEvaluator(Replace), true);
}
ReplaceAction Replace(object sender, ReplaceEvaluatorArgs e)
{
if (e.Match.Groups["name"].Value == "DIVHEAD")
{
e.Replacement = "DIVISION OF CARDIOLOGY ^p(518) 262-5076";
return ReplaceAction.Replace;
}
else
{
return ReplaceAction.Skip;
}
}
please give me some way to replace with paragraph charecter.The replacement string is constent string which we are taking from some other file resource, so string string may change or may contain more than one paragraph charecter.
Thanks and regards
vidhya thawale
Hi
Thanks for your inquiry. You can’t use special characters in the replacement. I think that you can try using the following code as workaround.
public void Test004()
{
// Open document
Document m_oOutDoc = new Document(@"Test004\in.doc");
Range m_oRange = m_oOutDoc.Range;
// Create rexeg
Regex regexFindPlaceholder = new Regex(@"<%(?\S+)%>");
// find and replace
m_oRange.Replace(regexFindPlaceholder, new ReplaceEvaluator(Replace), false);
// Save output document
m_oOutDoc.Save(@"Test004\out.doc");
}
ReplaceAction Replace(object sender, ReplaceEvaluatorArgs e)
{
if (e.Match.Groups["name"].Value == "DIVHEAD")
{
// Get MatchNode
Run run1 = (Run)e.MatchNode;
// Create Run
Run run2 = new Run(e.MatchNode.Document);
// Get index of match value
int index = run1.Text.IndexOf(e.Match.Value);
if (index > 0)
{
// Split text in match node
run2.Text = run1.Text.Substring(0, index);
run1.Text = run1.Text.Substring(index + e.Match.Value.Length);
}
else
{
run1.Text = run1.Text.Replace(e.Match.Value, "");
}
run1.ParentParagraph.InsertBefore(run2, run1);
DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);
// Move to run1
builder.MoveTo(run1);
// Insert value
builder.Write("DIVISION OF CARDIOLOGY \n(518) 262-5076");
return ReplaceAction.Skip;
}
else
{
return ReplaceAction.Skip;
}
}
Hope this helps.
Best regards.