How to do full text search in .doc files

Posted by Chaithragm under C# on 5/6/2013 | Points: 10 | Views : 1969 | Status : [Member] | Replies : 5
How to search the text which is present in .doc files,is their any third party tool ?




Responses

Posted by: Niladri.Biswas on: 5/6/2013 [Member] Platinum | Points: 25

Up
0
Down
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

Posted by: Chaithragm on: 5/6/2013 [Member] Starter | Points: 25

Up
0
Down
im using microsoft 20007 ...will this code work? its giving error

Assembly 'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' uses 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' which has a higher version than referenced assembly 'office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'
c:\Program Files\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Word.dll edocs(Windows)


Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niladri.Biswas on: 5/6/2013 [Member] Platinum | Points: 25

Up
0
Down
Hi, the find a compatible version. If you are using dotnet 3.5 (VS 2008), use Microsoft Word 12.0 Object Library...

I hope that will help.

Also change it to .doc instead of .docx.

hope this helps

Best Regards,
Niladri Biswas

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 5/6/2013 [Member] Starter | Points: 25

Up
0
Down
Hi sir Thanks for the response

when i search its giving error like test.docx is locked for editing by (sys_name)

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Yunda on: 7/16/2014 [Member] Starter | Points: 25

Up
0
Down
Yes, i am using TextFinding.com to do full text search in all types of files. It is a useful and easy text search tool.

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response