C # manipulating Word knowledge summary

Source: Internet
Author: User

1. Microsoft Official Example:

Paragraphs, tables, charts

How to: Use Visual C #. NET to make Word automatically create new documents

2. Learning Resources

(1) Word in the Office basic knowledge, must read, the following summary contains a summary of the content

http://msdn.microsoft.com/en-us/library/Aa201330

User Translated version: http://blog.csdn.net/hustliangchen/archive/2011/01/05/6118459.aspx

(2) Word class structure diagram, Application, document, Range, selection, etc., must read

http://msdn.microsoft.com/en-us/library/aa164012

(3) Word 2007 object model:

Http://msdn.microsoft.com/en-us/library/bb244515%28v=office.12%29.aspx

(4) Microsoft.Office.Interop.Word

Http://msdn.microsoft.com/zh-cn/library/ms254954%28v=Office.11%29.aspx

(5) WPS Two-time development interface Documentation Wpsapi.chm

Easy to read Chinese, csdn download

(6) Moth Word VBA Reference tutorial

All Chinese word class library, must read

http://www.feiesoft.com/vba/word/

(7) Improve word operation efficiency with VBA macros-20 classic instances to be lapping

Http://pcmag.hexun.com/2010-03-18/123031946.html

3. Some summaries

(1) The document represents a specific Word document, which is a collection of documents that represents a document by index. The ActiveDocument property is the document for the current focus. We generally do not use indexes to refer to documents because the index values change as the document is opened and closed, and we usually refer to it using the ActiveDocument property, or the Document object variable returned by the Add or open method of the Documents collection. The document of Add or open becomes activedocument, and the ActiveDocument method of the Document object is used if you want to make the other document a ActiveDocument.

Specify the specific documnet with the file name,Documents("Report.doc").Activate();

(2) characters composed of Words,words composed of sentences,sentences composed of paragraphs, so a document will contain such four sets: Characters Words, sentences, Paragraphs collection. In addition, document may also contain a collection of sections, and a section will have a HeadersFooters header and footer collection.

(3) Paragraph paragraph, consisting of a paragraph mark and all text. When you copy a paragraph, the paragraph formatting is also copied, if the paragraph mark is included. Do not copy the paragraph mark if you do not want to copy the format.

(3) A Range object, which represents a contiguous area defined by the starting and ending characters, can be as small as a single insertion cursor or as large as the entire document content, and it can be, but does not have to be, the area represented by the current selection. You can define multiple Range objects in a document.

We typically use the range class to define a variable to create a Range object, and then instantiate the Range object with the Range method of document or the Range property of other objects.

(4) Selection Object

can represent cursors

The object represents the current selection in the window or pane. The selection represents the area that is selected (or highlighted) in the document and represents the insertion point if no selection is in the document. Each document pane can have only one active Selection object, and there can be only one active Selection object in the entire application.
Using the Selection object
Use the Selection property to return the Selection object. If you do not use the object qualifier for the selection property, Word returns the selection in the active pane of the Active document window.

The Text property is its selected content

Copy, cut, paste methods for copying, cutting, pasting and other operations

(5) Sections

Sections.add method

The method is used to return a section object that represents the new sections added to the document.
Function ADD ([range as Range = 0], [Start as Wpssectionstart = 2]) as section

Parameter description
Range Variant type, optional. The area before which the section break is inserted. If this argument is omitted, the section break is inserted at the end of the document.
Start Variant type, optional. The type of section break to add. The Wpssectionstart type. If this argument is omitted, a section break of the next page type is added.
The Wpssectionstart type can be one of the following constants:
Value Description
Wpssectioncontinuous Continuous section break
Wpssectionevenpage even page section breaks
End of Wpssectionnewcolumn section
Wpssectionnewpage next page section break (default)
Wpssectionoddpage Odd Page section break

Sections Reference MSDN

Section Reference MSDN

4. Specific Use

(1) How to set the title style, "title One", "Title II" and other references

[C-sharp]View Plaincopy
  1. Public void AddTitle (string s)
  2. {
  3. //word paragraph
  4. Word.Paragraph p;
  5. p = oDoc.Content.Paragraphs.Add (ref missing);
  6. //Set content text in a paragraph
  7. P.range.text = s;
  8. //Set as a title
  9. object style = Word.WdBuiltinStyle.wdStyleHeading1;
  10. P.set_style (ref Style);
  11. //Add to end
  12. P.range.insertparagraphafter ();  //After you apply the InsertParagraphAfter method, the selection expands to include the new paragraph.
  13. }
  14. <summary>
  15. Add a normal paragraph
  16. </summary>
  17. <param name= "S" ></param>
  18. Public void Addparagraph (string s)
  19. {
  20. Word.Paragraph p;
  21. p = oDoc.Content.Paragraphs.Add (ref missing);
  22. P.range.text = s;
  23. object style = Word.WdBuiltinStyle.wdStyleBodyText;
  24. P.set_style (ref Style);
  25. P.range.insertparagraphafter ();
  26. }

(2) How to insert a table

Using the table class of word, someone also uses the DataTable class to assist

(3) How to insert a picture

InlineShapes is a resource such as a graphic embedded in Word

[C-sharp]View Plaincopy
  1. Public void Insertimage (string strpicpath, float picwidth, float picheight)
  2. {
  3. string FileName = Strpicpath;
  4. Object linktofile = false;
  5. Object savewithdocument = true;
  6. object Anchor = OWord.Selection.Range;
  7. OWord.ActiveDocument.InlineShapes.AddPicture (FileName, ref linktofile, ref savewithdocument, ref Anchor) .  Select ();
  8. OWORD.SELECTION.INLINESHAPES[1]. Width = Picwidth; //Picture width
  9. OWORD.SELECTION.INLINESHAPES[1]. Height = Picheight; //Picture height
  10. }

? Why is it gone again after inserting the picture?

This may be because you inserted the image after inserting the object, but you did not move the cursor, so the picture is covered out.

WORKAROUND: Cursor Movement

(4) Cursor movement

A: Tags:

System pre-defined Tags: object oendofdoc = "//endofdoc"; /*/endofdoc is a predefined bookmark system predefined bookmarks? */

Custom Tags:

B: Use the label to get the location

Word.Range wrdrng = ODoc.Bookmarks.get_Item (ref oEndOfDoc). Range;

This position is used when inserting paragraphs and tables:

OPara3 = ODOC.CONTENT.PARAGRAPHS.ADD (ref orng);

otable = ODoc.Tables.Add (wrdrng, 3, 5, ref omissing, ref omissing);

[C-sharp]View Plaincopy
  1. Go to a predefined bookmark, if the bookmark doesn ' t exists the application would raise an error
  2. public void Gotobookmark (string strbookmarkname)
  3. {
  4. //VB:Selection.GoTo What:=wdgotobookmark, name:= "Nome"
  5. Object Bookmark = (int) Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
  6. object namebookmark = Strbookmarkname;
  7. OWord.Selection.GoTo (ref Bookmark, ref missing, ref missing, ref Namebookmark);
  8. }
  9. public void Gototheend ()
  10. {
  11. //VB:Selection.EndKey unit:=wdstory
  12. object Unit;
  13. unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
  14. OWord.Selection.EndKey (ref unit, ref missing);
  15. }
  16. public void gotothebeginning ()
  17. {
  18. //VB:Selection.HomeKey unit:=wdstory
  19. object Unit;
  20. unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
  21. OWord.Selection.HomeKey (ref unit, ref missing);
  22. }

(5) Generate directory

[C-sharp]View Plaincopy
  1. Public void Insertcontent () //Generate table of contents with heading styles
  2. {
  3. Gotothebeginning ();
  4. object start = 0;
  5. object end = 0;
  6. Word.Range MyRange = OWord.ActiveDocument.Range (ref start, ref end); //Location area
  7. Object useheadingstyle = true; //Use head style
  8. object upperheadinglevel = 1; //Maximum first level
  9. object lowerheadinglevel = 3; //min. three level
  10. Object usehypelinks = true;
  11. Add a directory//tablesofcontents the Add method
  12. ODOC.TABLESOFCONTENTS.ADD (MyRange, ref useheadingstyle,
  13. ref upperheadinglevel, ref lowerheadinglevel,
  14. ref missing, ref missing, ref missing, ref missing,
  15. ref missing, ref usehypelinks, ref missing, ref missing);
  16. ODOC.TABLESOFCONTENTS[1]. UpdatePageNumbers (); //Update page number
  17. }
  18. #endregion

(6) How to set the directory format? such as bold, tilt, etc.

Use paragraph formatting

[C-sharp]View Plaincopy
  1. Public void FormatContent () {
  2. Word.tableofcontents mycontent = odoc.tablesofcontents[1]; //Directory
  3. Word.paragraphs myparagraphs = myContent.Range.Paragraphs; //All segments in the directory, one line
  4. int[] Firstparaarray = new int[3]{1, 8, 9}; //level headings, direct designation
  5. foreach (int i in firstparaarray) {
  6. Myparagraphs[i].  Range.Font.Bold = 1; //Bold
  7. Myparagraphs[i]. Range.Font.Name = "Blackbody"; //Font
  8. Myparagraphs[i]. Range.Font.Size = 12; //Small four
  9. Myparagraphs[i]. Range.ParagraphFormat.SpaceBefore = 6; //Pre-paragraph
  10. Myparagraphs[i]. Range.ParagraphFormat.SpaceAfter = 6; spacing after//segment
  11. }
  12. }

C # manipulating Word knowledge summary

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.