Hi,
In my sample Word document after I remove the fax number, I also need to remove the empty line left.
I appreciate if you show me how to do that.
F +44 20 586 08 88
doc.Range.Replace(fax.Substring(0,"F +44 20 586".Length + 6), "", false, true);
Regards,
Dave
Hi Dave,
Thanks for your inquiry. Please read following article for your kind reference.
Find and Replace
Please implement IReplacingCallback interface as shown below to get the desired output. Hope this helps you.
Document doc = new Document(MyDir + "DocSample.docx");
String fax = "F \\+44 20 586 07 82";
doc.Range.Replace(new Regex(fax), new FindAndReplace(), false);
doc.Save(MyDir + "Out.docx");
public class FindAndReplace : IReplacingCallback
{
string text;
public FindAndReplace(string text)
{
this.text = text;
}
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
Paragraph para = (Paragraph)e.MatchNode.ParentNode;
if (para.ToString(SaveFormat.Text).Trim().Equals(this.text))
// Remove the paragraph with the match text.
para.Remove();
else
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.MatchNode;
// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.MatchOffset > 0)
currentNode = SplitRun((Run)currentNode, e.MatchOffset);
ArrayList runs = new ArrayList();
// Find all runs that contain parts of the match string.
int remainingLength = e.Match.Value.Length;
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.GetText().Length <= remainingLength))
{
runs.Add(currentNode);
remainingLength = remainingLength - currentNode.GetText().Length;
// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.NextSibling;
}
while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
}
// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0))
{
SplitRun((Run)currentNode, remainingLength);
runs.Add(currentNode);
}
// remove run nodes
foreach (Run run in runs)
run.Remove();
}
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
///
/// Splits text of the specified run into two runs.
/// Inserts the new run just after the specified run.
///
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring(0, position);
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
}
Hi Tahir,
Why in my VS2012 project I get the error “No overload for method ‘ToString’ takes 1 arguments” at line:
if (para.ToString(SaveFormat.Text).Trim().Equals(this.text))
para.ToString() has no overloaded methode.
Dave
Ah I found it I was using 3.5 client. I just replaced it with 2.0
Hi Tahir,
Unfortunately your code is not working. It is not removing the fax number nor the empty line.
Why did you put back-slashes in fax number String fax = "F \+44 20 586 07 82";?
Besides your FindAndReplace() has no arguments.
Dave
Hi Dave,
Thanks for your inquiry.
Stibbedevelopers:
Why did you put back-slashes in fax number String fax = “F \+44 20 586 07 82”;?
The first parameter of Range.Replace method is System.Text.RegularExpressions.Regex. You need to create a valid Regex and pass it to Range.Replace method.
Stibbedevelopers:
Besides your FindAndReplace() has no arguments.
Please accept my apologies for your inconvenience.
Please check the following highlighted code snippet. You need to pass the string value which you want to remove from the document to the constructor of FindAndReplace. We have attached output document with this post for your kind reference.
Document doc = new Document(MyDir + "DocSample.docx");
String fax = "F \\+44 20 586 07 82";
doc.Range.Replace(new Regex(fax), new FindAndReplace("F +44 20 586 07 82"), false);
doc.Save(MyDir + "Out v16.6.0.docx");
Hi Tahir,
Thanks this code works but is not exactly what I meant. The fax number per document is different. So It can be +44 20 586 07 82 or +44 20 586 07 53 or +44 20 586 06 84 but the prefix is always the same +44 20 586 . So I do not know the complete fax number in advanced. I probably need to create a Regx pattern and then remove the entire fax line no matter what the complete fax number is. Can you show me how to do that?
Dave
Hi Dave,
Thanks for your inquiry.
If the Fax line only contains the fax number, please use following code example to remove the Paragraph that contains the fax number.
If there is some other text in the same paragraph, please use the code shared in my this post. In this case you need to create a Regx pattern according to your requirement.
Document doc = new Document(MyDir + "DocSample.docx");
doc.Range.Replace(new Regex("\\+44 20 586"), new FindAndReplace(), false);
doc.Save(MyDir + "Out v16.6.0.docx");
public class FindAndReplace : IReplacingCallback
{
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
Paragraph para = (Paragraph)e.MatchNode.ParentNode;
para.Remove();
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
}