C # Get the number of pages of a Word document, jump to the specified page to get the content, and insert a pagination character

Source: Internet
Author: User

Using MSWord = Microsoft. Office. InterOP. word;

MSWord. Application wordapp; // word application variable

MSWord. Document worddoc; // Word document variable

Object nothing = missing. value; // Initialization

Wordapp = new MSWord. applicationclass ();

Worddoc = wordapp. Documents. Add (ref nothing, ref nothing, refnothing, ref nothing); // create a word

 

// Open an existing word

Object filename = strpath;

Object readonly = false;

Object isvisible = true;

Worddoc = wordapp. documents. open (ref filename, ref nothing, refreadonly, ref nothing, ref isvisible, ref nothing, ref nothing, refnothing,
Ref nothing );


// Calculate the number of pages in a Word document

MSWord. wdstatistic stat = MSWord. wdstatistic. wdstatisticpages;

Int num = worddoc. computestatistics (stat, ref nothing );


// Jump to the specified bookmarks

Object what = MSWord. wdgotoitem. wdgotobookmark;

Object bookmarkname = "bookmark1 ";

Worddoc. activewindow. selection. Goto (ref what, ref nothing, refnothing, ref bookmarkname );

MessageBox. Show (worddoc. activewindow. selection. Paragraphs [1]. range. Text. tostring ());


// Jump to the specified page

Object what = MSWord. wdgotoitem. wdgotopage;

Object which = MSWord. wdgotodirection. wdgotonext;

Object Name = "1"; // Number of pages

Worddoc. activewindow. selection. Goto (ref what, ref which, refnothing, ref name); // The second parameter can use nothing

MessageBox. Show (worddoc. activewindow. selection. Paragraphs [1]. range. Text. tostring ());

MessageBox. Show (worddoc. activewindow. selection. Sentences [1]. Text. tostring ());

MessageBox. Show (worddoc. activewindow. selection. Paragraphs [1]. range. Sentences [1]. Text. tostring ());


MessageBox. Show (worddoc. content. Paragraphs [8]. range. Text. tostring ());
MessageBox. Show (worddoc. content. Paragraphs [8]. range. Sentences [1]. Text. tostring ());



// Insert paragraph and pagination

Word. Paragraph opara4;
Opara4 = mydoc. content. Paragraphs. Add (ref omissing );
Opara4.range. Text = "Figure 1 :";
Opara4.range. Font. Name = "";
Opara4.range. Font. size = 10.5f;
Opara4.range. insertparagraphafter ();
Insertchart (savedocpath); // insert an Excel-Chart
Opara4.range. insertparagraphafter ();

// Insert a paging character
Insertbreaknextpage ();


// Use a regular expression to segment a paragraph

String string2 = "section 1. \ R \ n section 2. \ R \ n section 3. ";
String [] paras = system. Text. regularexpressions. RegEx. Split (string2, @ "\ r \ n ");


// Insert a carriage return.

Word. selection. typeparagraph ();

// Insert a return character

Word. selection. typebackspace ();

// Jump to the end of the document

Object wd_story = word. wdunits. wdstory;

Word. selection. endkey (ref wd_story, ref omissing );

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.