C #. Net searches for Word documents in a specified folder using keywords

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.