Solution Explorer--references--(right-click) Add Reference--com
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 embedded interop type in the Microsoft.Office.Interop.Word attribute in the reference to False
Create a Word application
MSWord. Application WordApp = new Msword.applicationclass ();
Column:
private void Button1_Click (object sender, EventArgs e)
{
Create a Word application
Msword.application Wapp = new Msword.applicationclass ();
Set the application to be visible
Wapp. Visible = true;
Create an empty document
Msword.document MD = Wapp. Documents.Add ();
Create a document from a template
Msword.document MD = Wapp. Documents.Add ("E;\\ceshi.docx");
Open Document
Msword.document WordDoc = WordApp.Documents.Open (@ "E:\aaa.doc");
6. Add content to Word
Add content
Msword.application WordApp = new Msword.applicationclass ();
Msword.document WordDoc = WordApp.Documents.Add (); Create a blank
Paragraphs is a paragraph that can be directly add, here is the last range
WordDoc.Paragraphs.Last.Range.Text = "This is the first line \ n";//Application in the last append text
WordDoc.Paragraphs.Last.Range.Text = "The second line has started";// not added \ n will be overwritten
WordDoc.Paragraphs.Last.Range.Text = "will be overwritten";
Add segments directly, not overwrite
WordDoc.Paragraphs.Last.Range.Text + = "will not be covered";
Add text after this paragraph, not a new paragraph
WordDoc.Paragraphs.Last.Range. InsertAfter ("This is what follows");//append content after a document paragraph
wordapp.visible = true;//In addition to printing visible, other can not be seen
7. Save the document
Wordapp.visible = false; Non-visible Direct save
WordDoc. SaveAs2 (@ "E:\bbb.docx");//Save as save
Worddoc.close (TRUE);//Close Document
Wordapp.quit ();//Release the word process to exit the program
8. Manipulating fonts
Font
WordDoc.Paragraphs.Last.Range.Font.Bold = 1;
WordDoc.Paragraphs.Last.Range.Font.Italic = 1;
WordDoc.Paragraphs.Last.Range.Font.Size = 32;
WordDoc.Paragraphs.Last.Range.Font.Name = "in italics";
WordDoc.Paragraphs.Last.Range.Font.Underline = MsWord.WdUnderline.wdUnderlineDash;
WordDoc.Paragraphs.Last.Range.Font.ColorIndex = MsWord.WdColorIndex.wdPink;
9. Create a table
Table, 5 rows, 5 columns
Msword.table Table = WORDDOC.PARAGRAPHS.LAST.RANGE.TABLES.ADD (worddoc.paragraphs.last.range,5,5);
Set Edge Visibility
Table. 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 (int i =1; i<=5;i++)
{
for (int j = 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 = 200;
WORDDOC.INLINESHAPES[1]. Height = 150;
Scale size by scaling
WORDDOC.INLINESHAPES[1]. ScaleWidth = 30;
WORDDOC.INLINESHAPES[1]. ScaleHeight = 30;
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 = 80;
worddoc.bookmarks["Zhaopian"]. RANGE.INLINESHAPES[1]. Height = 100;
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
Wordapp.visible = false;
Worddoc.close (FALSE);
Wordapp.quit ();
winform--working with Word documents