Add reference: Microsoft Word 11.0 object liabrary under com
Private Static object nullobj = system. reflection. Missing. value;
/** // <Summary>
/// Search in a DOC file (query the content of the DOC file)
/// </Summary>
/// <Param name = "FILENAME"> </param>
/// <Param name = "searchfortext"> text to be searched </param>
/// <Param name = "casesensitive"> case sensitive </param>
/// <Returns> </returns>
Public static bool searchindoc (string filename, string searchfortext, bool casesensitive)
{
Bool result = (searchfortext. Length = 0 );
Object filename = filename; // path of the file to be opened
Object missingvalue = system. reflection. Missing. value; // type. missing;
Object readonly = false;
Microsoft. Office. InterOP. Word. Application Wp = NULL;
Microsoft. Office. InterOP. Word. Document WD = NULL;
Try
{
WP = new Microsoft. Office. InterOP. Word. applicationclass ();
WD = WP. Documents. Open (ref filename, ref missingvalue,
Ref readonly, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue );
Int I = 0, icount = 0;
Microsoft. Office. InterOP. Word. Find wfnd;
If (WD. Paragraphs! = NULL & WD. Paragraphs. Count> 0)
{
Icount = WD. Paragraphs. count;
For (I = 1; I <= icount; I ++)
{
Wfnd = WD. Paragraphs [I]. range. Find;
Wfnd. clearformatting ();
Wfnd. matchcase = casesensitive;
Wfnd. Text = searchfortext;
If (wfnd. Execute (ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue,
Ref missingvalue ))
{
Result = true;
Break;
}
}
}
}
Catch (exception ex)
{
Throw new exception (ex. Message );
}
Finally
{
If (WD! = NULL)
{
WD. Close (ref nullobj, ref nullobj, ref nullobj );
System. runtime. interopservices. Marshal. releasecomobject (WD );
WD = NULL;
}
If (WP! = NULL)
{
WP. Quit (ref nullobj, ref nullobj, ref nullobj );
System. runtime. interopservices. Marshal. releasecomobject (WP );
WP = NULL;
}
GC. Collect ();
}
Return result;
}