Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to search the text which is present in .doc files,is their any third party tool ?
Posted
Updated 5-May-13 19:51pm
v2

Why 3rd party? Let's start from 1st party. Don't expect to find the search, you can implement it by yourself when you get access to document context.

First, you can use Office Interop, Microsoft.Office.Interop.Word. However, it requires Office installation on a target system. Please see:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word%28v=office.11%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dd264733.aspx[^].

Alternatively, you can use Open XML SDK:

http://www.microsoft.com/en-us/download/details.aspx?id=30425[^].

This way, you can support new XML-based Office formats (such as .DOCX, .XLSX), ECMA-376 standard:
http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats[^],
http://en.wikipedia.org/wiki/Office_Open_XML[^].

This way, you can work without Office installed. Also, the documents are supported by 3rd-party software. Please see my past answers:
Convert Office-Documents to PDF without interop[^],
Need a rather unique WPF text editor control[^],
Hi how can i display word file in windows application using c#.net[^],
Read a word file without using Interop.word dll...Do not want to install word in IIS..[^].

This is another option: http://npoi.codeplex.com/[^].

See also: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^].

—SA
 
Share this answer
 
Comments
Maciej Los 6-May-13 2:06am    
Complete answer!
+5
Sergey Alexandrovich Kryukov 6-May-13 2:13am    
Thank you, Maciej.
—SA
public int word_search(String wordpath)
{


string toSearch;
toSearch = content;
List<string> wordCollection = new List<string>();
object fileName;// "D:\\file.docx";
fileName = wordpath;
object readOnly = false;
object isVisible = true;
object parammissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.
Document oWordDoc = null;
Microsoft.Office.Interop.Word.
Application oWordApp = new Microsoft.Office.Interop.Word.Application();
try
{
oWordDoc = oWordApp.Documents.Open(
ref fileName,
ref parammissing, ref readOnly,
ref parammissing, ref parammissing, ref parammissing,
ref parammissing, ref parammissing, ref parammissing,
ref parammissing, ref parammissing, ref isVisible,
ref parammissing, ref parammissing, ref parammissing);


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)
{

res = 1;

}
else
{
res = 0;
}

}
catch (Exception ex)
{
}
finally
{
// Close and release the Document object.
if (oWordDoc != null)
{
oWordDoc.Close(
ref parammissing, ref parammissing, ref parammissing);
oWordDoc =
null;
}
// Quit Word and release the ApplicationClass object.
if (oWordApp != null)
{
oWordApp.Quit(
ref parammissing, ref parammissing, ref parammissing);
oWordApp =
null;
}

GC.Collect();
GC.WaitForPendingFinalizers();

}
return res;
}
 
Share this answer
 
I have used this code but after searching its giving error like test.docx is locked for editing by (sys_name)
how to solve this
public void wordsearch()
{

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();


}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900