Office programming is essential.

Source: Internet
Author: User

1. Official Microsoft instance:

Paragraphs, tables, and charts

How to: Use Visual C #. net to automatically create a document in Word

 

2. Learning Resources

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

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

Http://blog.csdn.net/hustliangchen/archive/2011/01/05/6118459.aspx:

(2) structure of the Word class, including Application, document, Range, and Selection, which must be read

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

(3) word 2007 object model:

Http://msdn.microsoft.com/en-us/library/bb244515 (v = office.12). aspx

(4) Microsoft. Office. Interop. Word

Http://msdn.microsoft.com/zh-cn/library/ms254954 (v = Office.11). aspx

(5) WPS secondary development interface documentation wpsapi. chm

Easy to read Chinese and download CSDN

(6) moth Word VBA reference tutorial

All Chinese word class libraries, must read

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

(7) using VBA macros to improve Word operation efficiency-20 classic instances that require excellent research

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

 

3. Summary

(1) Document represents a specific word Document. Documents are a collection of Documents and are represented by an index. The ActiveDocument attribute is the document of the current focus. We generally do not use indexes to reference documents, because the index value changes as the document is opened or closed. Usually we use the ActiveDocument attribute, or the document Object variable returned by the Add or Open method of the Documents set is referenced. Add or Open document will become ActiveDocument. To make other documents activeDocument, use the ActiveDocument method of the document object.

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

 

(2) characters form words, words form sentences, and sentences form paragraphs. Therefore, a document contains four collections: Characters Words, Sentences, and Paragraphs collection. In addition, the document may contain a set of sections, and a section contains a set of HeadersFooters headers and footers.

 

(3) a Paragraph consists of a Paragraph sign and all texts. When a paragraph is copied, if the paragraph logo is included, the paragraph format is also copied. If you do not want to copy the paragraph logo, do not copy the paragraph logo.

 

(3) the Range object represents a continuous area defined by the start character and end character. It can be as small as one insert cursor or as large as the entire document content, it can also be but not necessarily the region represented by the current selection. Multiple Range objects can be defined in a document.

We usually use the Range class to define a variable to create a Range object, and then use the Range method of Document or the Range attribute of other objects to instantiate this Range object.

 

(4) Selection object

Can represent the cursor

This object represents the currently selected content in the window or pane. The selected content indicates the selected (or highlighted) area in the document. If no selected content exists in the document, it indicates the insertion point. Each document pane can have only one active Selection object and only one active Selection object in the application.
Use Selection object
You can use the Selection attribute to return the Selection object. If no object identifier is used for the Selection attribute, Word returns the selected content from the activity pane in the activity document window.

The Text attribute is the selected content.

Copy, Cut, and Paste operations

 

(5) sections

Sections. Add method

This method is used to return a Section object, which indicates the new Section 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 delimiter is inserted. If this parameter is ignored, the delimiter is inserted at the end of the document.
Start Variant type. Optional. The delimiter type to add. WpsSectionStart type. If this parameter is ignored, a "next page" type is added.
The WpsSectionStart type can be one of the following constants:
Value description
WpsSectionContinuous continuous delimiter
WpsSectionEvenPage even number of page splitters
End of wpsSectionNewColumn
WpsSectionNewPage next page delimiter (default)
WpsSectionOddPage

Sections refer to MSDN

Section refer to MSDN

 

4. Specific use

(1) how to set the title style, refer to "Title 1" and "Title 2"

[C-sharp]View plaincopyprint?
  1. Public void AddTitle (string s)
  2. {  
  3. // Word paragraph
  4. Word. Paragraph p;
  5. P = oDoc. Content. Paragraphs. Add (ref missing );
  6. // Set the content text in the paragraph
  7. P. Range. Text = s;
  8. // Set it to Heading 1
  9. Object style = Word. WdBuiltinStyle. wdStyleHeading1;
  10. P. set_Style (ref style );
  11. // Add to the end
  12. P. Range. InsertParagraphAfter (); // after the InsertParagraphAfter method is applied, the selected content is extended to include the new paragraph.
  13. }  
  14. ///
  15. /// Add a common section
  16. ///
  17. ///
  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. }  

/// Add a common section // public void AddParagraph (string s) {Word. paragraph p; p = oDoc. content. paragraphs. add (ref missing); p. range. text = s; object style = Word. wdBuiltinStyle. wdStyleBodyText; p. set_Style (ref style); p. range. insertParagraphAfter ();}

 

(2) how to insert a table

Using the Table class of Word, some people also use the DataTable class for assistance

 

(3) how to insert images

InlineShapes is an embedded graph resource in Word.

[C-sharp]View plaincopyprint?
  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; // the image Width.
  9. OWord. Selection. InlineShapes [1]. Height = picHeight; // The Image Height.
  10.         }  

 

? Why does the image disappear after it is inserted?

This may be because you have inserted an image, but you have not moved the cursor, so the image is overwritten.

Solution: Move the cursor

 

(4) move the cursor

A: tag:

System pre-defined tag: object oEndOfDoc = "// endofdoc ";

Custom tag:

B: use tags to obtain 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 plaincopyprint?
  1. // Go to a predefined bookmark, if the bookmark doesn't exists the application will 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 a directory

[C-sharp]View plaincopyprint?
  1. Public void insertContent () // use the title style to generate a directory
  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 the Head style
  8. Object upperHeadingLevel = 1; // maximum level
  9. Object lowerHeadingLevel = 3; // minimum level 3
  10. Object useHypeLinks = true;
  11. // Add a Directory using the Add method of TablesOfContents
  12. ODoc. TablesOfContents. Add (myRange, ref useHeadingStyle,
  13. Ref upperHeadingLevel, ref lowerHeadingLevel,
  14. Ref missing,
  15. Ref missing, ref useHypeLinks, ref missing, ref missing );
  16. ODoc. TablesOfContents [1]. UpdatePageNumbers (); // update the page number
  17.         }  
  18. # Endregion

 

(6) How to set the directory format? For example, bold or skewed

Use paragraph format settings

[C-sharp]View plaincopyprint?
    1. Public void formatContent (){
    2. Word. TableOfContents myContent = oDoc. TablesOfContents [1]; // Directory
    3. Word. Paragraphs myParagraphs = myContent. Range. Paragraphs; // all the segments in the directory, one row
    4. Int [] FirstParaArray = new int [3] {1, 8, 9}; // level 1 title, which is directly specified
    5. Foreach (int I in FirstParaArray ){
    6. MyParagraphs [I]. Range. Font. Bold = 1; // Bold
    7. MyParagraphs [I]. Range. Font. Name = ""; // Font
    8. MyParagraphs [I]. Range. Font. Size = 12; // small 4
    9. MyParagraphs [I]. Range. ParagraphFormat. SpaceBefore = 6; // Before
    10. MyParagraphs [I]. Range. ParagraphFormat. SpaceAfter = 6; // segment spacing
    11.             }  

Office programming is essential.

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.