Hello,
While I see lots of documentation regarding how to find and replace, I can’t seem to find any documentation on how to ‘Find’. I have a Document and I want to check if a string exist in the Document prior to further processing. How Can I do that?
This replaces: doc.Range.Replace(oldText, newText, false, false);
Hi Anoop,
Thanks for your inquiry, Im afraid you have to apply some custom logic for the requirement.
kindly look into the following code snippet may be it will fit in your scenario. Please feel free to contact us for any further assistance. we are always glad to help you.
// We want to replace/Find "your document" phrase.
Regex regex = new Regex("your document", RegexOptions.None);
// We replace regex expression with same expression mean "your document" as replace method return no of matches found & replaced as int
if ((doc.Range.Replace(regex, "your document"))> 0)
// add your logic here
Regards,
Tilal Ahmad
Hi
Thanks for your request. I would advise you to use IReplacingCallback in this case:
[Test]
public void Test001()
{
Document doc = new Document(@"C:\Temp\in.doc");
OccurrencesCounter counter = new OccurrencesCounter();
doc.Range.Replace(new Regex("test"), counter, false);
Console.WriteLine(counter.Occurrences);
}
private class OccurrencesCounter: IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs args)
{
mOccurrences++;
return ReplaceAction.Skip;
}
public int Occurrences
{
get
{
return mOccurrences;
}
}
private int mOccurrences;
}
Hope this helps.
Best regards,