1. Install Office, add a reference to COM inside Microsoft Word 14.0 Object. Library
2. Guide namespace using MSWord =microsoft.office.interop.word;
3. Create a Word application
Set the embedding interop in the Microsoft.Office.Interop.Word attribute in the reference to False
Create a Word application
New Msword.applicationclass ();
Make it visible:
true;
4. Create a Word Document object:
Msword.document WordDoc = WordApp.Documents.Add (); Create an empty document
Create a new document based on the AAA template
Msword.document WordDoc = WORDAPP.DOCUMENTS.ADD (@ "E:\aaa.doc");
5. Open the Word document
Msword.document WordDoc = WordApp.Documents.Open (@ "E:\aaa.doc");
6. add content to Word
//Add ContentMsword.application WordApp =NewMsword.applicationclass (); Msword.document WordDoc=WordApp.Documents.Add (); //paragraphs is a paragraph that can be directly add, here is the last rangeWordDoc.Paragraphs.Last.Range.Text ="This is the first line \ n"; WordDoc.Paragraphs.Last.Range.Text="the second line starts.";//No, it'll be overwritten .WordDoc.Paragraphs.Last.Range.Text ="will overwrite the"; //add segments directly, not overwriteWordDoc.Paragraphs.Last.Range.Text + ="will not overwrite the"; //add text after this paragraph, not a new paragraphWordDoc.Paragraphs.Last.Range.InsertAfter ("This is what's behind it."); Wordapp.visible=true;
7. Save
false // not visible directly save worddoc.saveas2 (@ "E:\bbb.docx"); // Save Worddoc.close (true); // close wordapp.quit (); // Release the word process
8. Manipulating fonts
// Font 1 ; 1 ; + ; " italics " ; = MsWord.WdUnderline.wdUnderlineDash; = MsWord.WdColorIndex.wdPink;
9. Create a table
//table, 5 rows, 5 columnsMsword.table Table = WordDoc.Paragraphs.Last.Range.Tables.Add (WordDoc.Paragraphs.Last.Range,5,5); //Set Edge VisibilityTable. Borders.Enable =1; table. Borders.insidecolor=msword.wdcolor.wdcolorblue;table. Borders.outsidecolor=msword.wdcolor.wdcolorblack;table. Borders.outsidelinewidth=MsWord.WdLineWidth.wdLineWidth025pt;//form plus Content for(inti =1; i<=5; i++){ for(intj =1; J <=5; J + +) {table. Rows[i]. CELLS[J]. Range.Text="line I, column J";}
The following line of code sets the background color of the table row
Table. Rows[i]. Shading.backgroundpatterncolor = MSWord.WdColor.wdColorDarkYellow;
10. How do I insert a picture?
WordDoc.InlineShapes.AddPicture (@ "E:\cyl.jpg",false,true); // set the absolute size of the picture's width height // Worddoc.inlineshapes[1]. Width = $; // Worddoc.inlineshapes[1]. Height = max; // proportionally scaled small worddoc.inlineshapes[1worddoc.inlineshapes[1 ;
11. How to manipulate bookmarks and add content at bookmarks
worddoc.bookmarks["xingming"]. Range.Text ="Banjarmasin"; worddoc.bookmarks["Xingbie"]. Range.Text ="male"; worddoc.bookmarks["Minzu"]. Range.Text ="Han"; worddoc.bookmarks["Shengri"]. Range.Text ="1991-6-1"; worddoc.bookmarks["Zhengzhimianmao"]. Range.Text ="League member"; worddoc.bookmarks["Zhaopian"]. Range.InlineShapes.AddPicture (@"e:\cyl.jpg"); worddoc.bookmarks["Zhaopian"]. range.inlineshapes[1]. Width = the; worddoc.bookmarks["Zhaopian"]. range.inlineshapes[1]. Height = -;
12. Print Preview and Print
Print Preview:
Worddoc.printpreview ();
Print
Worddoc.printout ();
13. Note: When hiding, be sure to note that the code is last written on close the document and program
false ; Worddoc.close (false); Wordapp.quit ();
C # Action Word