C # operate Word documents (encryption, decryption, and insert pagination characters to corresponding bookmarks)

Source: Internet
Author: User
Tags password protection

Recently, in a project, the customer requested to display the content in the generated RTF file by page. As he had no idea about this, he also found relevant information on the Internet, in addition, I used my own search on msdn to solve the problem. The following is a simple summary of C # Word documents (encryption, decryption, and pagination). I hope to help some friends. Pai_^

Before writing code, you need to reference the corresponding DLL file:

1. InterOP. Microsoft. Office. InterOP. Word. dll (available for download online)

2. mscorlib. dll (add reference --->. Net)

1 using Microsoft. office. interOP. word; 2 using MSWord = Microsoft. office. interOP. word; 3 using system. reflection; 4 5 private void button#click (Object sender, system. eventargs e) 6 {7 // Word document password 8 string pass = "[email protected]"; 9 object Password = pass; 10 MSWord. application wordapp; // word application variable 11 MSWord. document worddoc; // Word document variable 12 try13 {14 object nothing = missing. value; // initialize 15 wordap P = new MSWord. applicationclass (); 16 17 // open an existing word18 object filename = @ "E: \ archive \ CMPLatest_2117_230614-1053.Rtf"; 19 object readonly = false; 20 object isvisible = true; 21 object objfalse = false; 22 23 worddoc = wordapp. documents. open (ref filename, ref nothing, ref readonly, ref nothing, ref password, ref nothing, ref isvisible, Ref nothing, ref nothing); 24 25 // activate Word document 26 worddoc. activate (); 27 // determine whether the password is 28 If (worddoc. haspassword) 29 {30 worddoc. password = NULL; 31} 32 33 // check whether the word document is protected. If no protection function is set, remove password protection 34 if (worddoc. protectiontype! = Wdprotectiontype. wdnoprotection) 35 {36 worddoc. unprotect (ref password); 37} 38 39 // jump to the specified bookmarks 40 object tomark = MSWord. wdgotoitem. wdgotobookmark; 41 // The paging character 42 object opagebreak = Microsoft. office. interOP. word. wdbreaktype. wdpagebreak; 43 44 // define the bookmarkname partb45 object bookmarkname_ B = "bmf_ B"; 46 worddoc. activewindow. selection. goto (ref tomark, ref nothing, ref nothing, ref bookmarkname_ B); 47 // insert a paging character 48 worddo C. activewindow. selection. insertbreak (ref opagebreak); 49 50 // define the bookmarkname partc151 object bookmarkname_c1 = "bmf_c1"; 52 worddoc. activewindow. selection. goto (ref tomark, ref nothing, ref nothing, ref bookmarkname_c1); 53 // insert a page break 54 worddoc. activewindow. selection. insertbreak (ref opagebreak); 55 56 // define the bookmarkname partc257 object bookmarkname_c2 = "bmf_c2"; 58 worddoc. activewindow. selection. goto (ref tomark, ref nothin G, ref nothing, ref bookmarkname_c2); 59 // insert a paging character 60 worddoc. activewindow. selection. insertbreak (ref opagebreak); 61 62 // encryption for Word documents 63 If (password. tostring ()! = NULL) 64 {65 worddoc. protect (wdprotectiontype. wdallowonlyreading, ref objfalse, ref password, ref nothing, ref nothing); 66} 67 68 69 // save 70 worddoc for the Word document after the pagination character is inserted. saveas (ref filename, ref nothing, ref objfalse, ref nothing, ref isvisible, ref nothing, ref nothing); 71 72 // mark as the final state. The dialog box 73 // wordapp is not displayed. displayalerts = wdalertlevel. wdalertsnone; 74 // mark as final status 75 // worddoc. final = true; 76 77 // close Word document 78 worddoc. close (ref nothing, ref nothing, ref nothing); 79 wordapp. quit (ref nothing, ref nothing, ref nothing); 80} 81 catch (exception ex) 82 {83 84} 85}

 

Related Article

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.