Search Word Doc for specific words

Today we did purchase ASPOSE word license, can you please advise how to search word documents for specific words using c#.

Thanks,

We need to just search not replace. And make a list of word documents which has specific Text in the document.

@rajanindia You can use Find/Replace functionality to achieve this. In your case you can implement IReplacingCallback to skip replacing. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
SkipReplacingCallback callback = new SkipReplacingCallback();
opt.ReplacingCallback = callback;
doc.Range.Replace("test", "", opt);
Console.WriteLine($"The document contains {callback.Occurrences} occurrences of 'test'");
private class SkipReplacingCallback : IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        mOccurrences++;
        return ReplaceAction.Skip;
    }

    public int Occurrences
    {
        get { return mOccurrences; }
    }

    private int mOccurrences;
}

Thanks Alexey for quick reply.

Warm regards,
Rajan

1 Like