Use C # To search text in Word documents

Source: Internet
Author: User
Tags string indexof
Application in Word Program Searching and replacing text in a word is just a breeze. Using the word object model, we can also use programming.

The object model of word has detailed help documents, which are placed in the office installer directory. Office 2003 is under Program Files \ Microsoft Office \ office11 \ 2052, which is provided for VBA, you can also see the VBA help of all office applications in this directory.

Open vbawd10.chm and see the word object model. Based on previous experience, it is easy to find the content attribute under the Document Object. This attribute returns a range object in the text part of the document, it is not difficult to get all the document content from this object, and then use the string indexof () method to easily achieve the goal.

Object filename = ""; // path of the document to be opened
String strkey = ""; // text to be searched
Object missingvalue = type. missing;

Word. Application Wp = new word. applicationclass ();
Word. Document WD = WP. Documents. Open (ref filename, 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 );

If (WD. content. Text. indexof (strkey)> = 0)
{
MessageBox. Show ("the document contains the specified keyword! "," Search result ", messageboxbuttons. OK );
}
Else
{
MessageBox. Show ("this document does not contain the specified keyword! "," Search result ", messageboxbuttons. OK );
}


However, this approach is barely feasible. For small documents, there is no problem. For ultra-long and super-large documents, such an implementation method has hidden bugs, it is also a program-level bug, because it is difficult to find problems during normal testing, and it is difficult to quantify the results of the program during use.

In fact, the word already provides the find object that can be used as the search object, which is easy to find in the object model. The corresponding description is as follows: This object represents the execution condition of the search operation. The properties and methods of the find object are the same as those in the "replace" dialog box. From the model perspective, the find object is a member of selection, from the exampleCodeIt seems that it is also a member of the range. Look for the attribute of the range. Modify the above Code:

WD. content. Find. Text = strkey;
If (WD. content. Find. 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 ))
{
MessageBox. Show ("the document contains the specified keyword! "," Search result ", messageboxbuttons. OK );
}
Else
{
MessageBox. Show ("this document does not contain the specified keyword! "," Search result ", messageboxbuttons. OK );
}

This does not seem to be the best, because I only need to judge whether the specified text is in the document, but do not need to know how many times it appears. If there are multiple texts to be searched, is it true that all documents are searched every time? Suppose that the text I want to search for is included in the document. It is best to include the text I want to search for at the beginning of the document, the worst case is to include the text to be searched at the end of the document. If you take a part of the document for judgment each time, the search will end when the condition is met, so that you can avoid searching the entire document each time. The paragraphs object in the model now comes in handy. modify the code:

Int I = 0, icount = 0;
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. Text = strkey;
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 ))
{
MessageBox. Show ("the document contains the specified keyword! "," Search result ", messageboxbuttons. OK );
Break;
}
}
}

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.