The following process prints the first page of the activity document:'Visual basicfriend sub printoutdoc () thisdocument. printout (_ Background: = true, _ append: = false, _ range: = word. wdprintoutrange. wdprintcurrentpage, _ item: = word. wdprintoutitem. wdprintdocumentcontent, _ copies: = "1", _ pages: = "1", _ pagetype: = word. wdprintoutpages. wdprintallpages, _ printtofile: = false, _ collate: = true, _ manualduplexprint: = false) end sub // C # Internal void printoutdoc () {object Mytrue = true; object myfalse = false; object missingvalue = type. missing; object range = word. wdprintoutrange. wdprintcurrentpage; object items = word. wdprintoutitem. wdprintdocumentcontent; object copies = "1"; object pages = "1"; object pagetype = word. wdprintoutpages. wdprintallpages; thisdocument. printout (ref Mytrue, ref myfalse, ref range, ref missingvalue, ref items, ref copies, ref pages, ref pagetype, ref myfalse, ref Mytrue, ref missingvalue, ref myfalse, ref missingvalue, ref missingvalue );}
How to: Search for text in a Word document
FindObjectSelectionAndRangeYou can use any of the two to search for text. The "replace" command is an extension of the "Search" command.
UseSelectionWhen the object searches for text, all the specified search conditions only apply to the selected text. IfSelectionIs the insertion point, the entire document is searched. If you find an item that matches the search criteria, select the item that will be automatically changed to highlight the found item.
Use selection object to find text
- The following process searches for the string "Find me ". After finding the first one, it highlights the word and displays a message box.
'Visual basicfriend sub selectionfind () dim strfind as string = "Find me" with thisapplication. selection. find. clearformatting (). TEXT = strfind if. execute = true then MessageBox. show ("text found. ") else MessageBox. show ("the text cocould not be located. ") end if end withend sub // C # Internal void selectionfind () {string strfind =" Find me "; word. find FND = thisapplication. selection. find; fnd. clearformatting (); fnd. TEXT = strfind; object missingvalue = type. missing; If (FND. execute (ref missingvalue, ref missingvalue, ref missingvalue, ref missingvalue, ref missingvalue) {MessageBox. show ("text found. ");} else {MessageBox. show ("the text cocould not be located. ");}}
Note that,FindThe condition is cumulative, which means that a condition will accumulate with the previous search condition. You can use. Clearformatting ()Method to clear the format settings for the last search.
UseRangeObject Search Text allows you to search for text without displaying any content on the user interface. If you find the text that matches the search criteria,FindMethod returns true; otherwise, false. If the text is found, the method will be redefined.RangeObject To match the search criteria.
Use the range object to search for text
- DefineRangeObject.
'Visual basicdim RNG as word. range = thisdocument. Paragraphs (2). Range/C # Word. Range RNG = thisdocument. Paragraphs [2]. range;
- UseFindMethod, first clear any existing format setting options, and then search for the string "Find me ".
'Visual basicwith RNG. find. clearformatting () If. execute (findtext: = "Find me") Then // C # word. find FND = RNG. find; fnd. clearformatting (); object missingvalue = type. missing; object findstr = "Find me"; if (FND. execute (
- Display the search result in the message box, and finally selectRangeTo make it visible.
'Visual basic MessageBox. show ("text found. ") else MessageBox. show ("text not found. ") end ifend withrng. select () // C # {MessageBox. show ("text found. ");} else {MessageBox. show ("text not found. ");} RNG. select ();
If the search fails, the second segment is selected. if the search is successful, the search criteria are displayed.
The complete method is as follows:
'Visual basicfriend sub rangefind () dim RNG as word. range = _ thisdocument. paragraphs (2 ). range with RNG. find. clearformatting () If. execute (findtext: = "Find me") Then MessageBox. show ("text found. ") else MessageBox. show ("text not found. ") end if end with RNG. select () end sub // C # Internal void rangefind () {word. range RNG = thisdocument. paragraphs [2]. range; word. find FND = RNG. find; fnd. clearformatting (); object missingvalue = type. missing; object findstr = "Find me"; if (FND. execute (ref findstr, ref missingvalue, ref missingvalue, ref missingvalue, ref missingvalue, ref missingvalue) {MessageBox. show ("text found. ");} else {MessageBox. show ("text not found. ");} RNG. select ();}