I am using dotnet4.0.
Step 1: Click on the COM tab and look for the Microsoft Word 14.0 Object Library.Add it.
Step 2: Create a word file(test.docs) that will have some content as
(Hello,
How r u today?)
Step 3: Add the below code
static void Main(string[] args)
{
string toSearch = "How";
List<string> wordCollection = new List<string>();
object fileName = "D:\\test.docx";
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application oWordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName,
ref missing, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible,
ref missing, ref missing, ref missing);
for (int i = 0; i < oWordDoc.Paragraphs.Count; i++)
{
wordCollection.Add(oWordDoc.Paragraphs[i + 1].Range.Text.ToString());
}
var foundCollection = wordCollection.FindAll(i => i.Contains(toSearch));
if(foundCollection.Count > 0)
Console.WriteLine("text found");
else
Console.WriteLine("text not found");
Console.ReadKey();
}
Hope this will help.
Best Regards,
Niladri Biswas
Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator