// The following are all my little cainiao debugging in vs2008,
// I would like to share with you
// Remember to add an application
// Add an application by right-clicking the application button in resource manager, and then finding Microsoft. offoce. word.11 in. net
// Wish you a pleasant learning experience ....
Using system;
Using system. IO;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using MSWord = Microsoft. Office. InterOP. Word; // defines a MSWord
Create a Word document in namespace and fill it with content
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private MSWord. Application wordapp; // defines a wordapp Application
Private MSWord. Document worddoc; // defines a worddoc document
Private void button#click (Object sender, eventargs E)
{
Object nothing = system. reflection. Missing. value; // defines a missing
Wordapp = new MSWord. applicationclass (); // initialize wordapp
Worddoc = wordapp. Documents. Add (ref nothing, ref nothing); // initialize a worddoc
Object filename = "F: \ aa.doc ";
If (file. exists (filename. tostring () // checks whether the object exists.
{
File. Delete (filename. tostring (); // delete the file if it exists.
}
Wordapp. Visible = true; // set the dynamically created Word file to be visible
Wordapp. selection. pagesetup. leftmargin = wordapp. centimeterstopoints (float. parse ("2"); // set the left margin of the Word Document
Wordapp. selection. pagesetup. rightmargin = wordapp. centimeterstopoints (float. parse ("2"); // set the right margin of a Word document
Wordapp. activewindow. horizontalpercentscrolled = 11; // sets the horizontal sliding distance of the document.
Wordapp. activewindow. View. type = MSWord. wdviewtype. wdoutlineview; // you can specify the header type of a document.
Wordapp. activewindow. View. seekview = MSWord. wdseekview. wdseekcurrentpageheader; // set that the header of the document is displayed in the document header.
Wordapp. activewindow. activepane. selection. insertafter ("this is my header content ");
Wordapp. selection. paragraphformat. Alignment = MSWord. wdparagraphalignment. wdalignparagraphright; // set the alignment of the header content
// Wordapp. activewindow. View. seekview = MSWord. wdseekview. wdseekmaindocument; // jump out of the header settings
Wordapp. selection. paragraphformat. linespacing = 11f; // set the section distance to 11f.
MSWord. Paragraph para1; // defines a paragraph as para1
Para1 = worddoc. Paragraphs. Add (ref nothing); // initialize para1
Para1.range. TEXT = "this is my content 2 This is my content 2 This is my content 2 This is my content 2 This is my content 2 This is my content 2 This is my content 2 This is my content 2 This is my content content 2: This is my content 2 "; // Add the section para1
Para1.format. characterunitfirstlineindent = 2; // you can specify the indentation of the first line.
Para1.range. Font. Color = MSWord. wdcolor. wdcolorblue; // set the font color.
Para1.format. spaceafter = 6; // set the null behavior 6 after the section para1
Para1.range. insertparagraphafter (); // Add section para1 to the document
MSWord. Paragraph para2; // same as above
Object endofdoc = "\ endofdoc"; // defines the label endofdoc. "endofdoc" indicates the bookmarks at the end of the document.
Object range1 = worddoc. bookmarks. get_item (ref endofdoc). range; // locate the bookmarks of the document.
Para2 = worddoc. Paragraphs. Add (ref range1); // same as above
Para2.range. Text = "asfdasdfasdfasdffasdfsadfasdfasfdasdfas"; // same as above
Para2.range. insertparagraphafter (); // same as above
MSWord. Paragraph para3; // same as above
Object range2 = worddoc. bookmarks. get_item (ref endofdoc). range; // same as above
Para3 = worddoc. Paragraphs. Add (ref range2); // same as above
Para3.range. Text = "eeeeeeeeeeeeeeeeeeeeeeeeee"; // same as above
Object STTAR = (object) (para3.range. Start + 2); // "para3.range. Start" indicates that two characters are added at the beginning of the para3 section.
Object eend = (object) (para3.range. Start + 2); // same as above
Worddoc. Range (ref STTAR, ref eend). insertbefore ("TTT"); // Add two characters at the beginning of the para3 section and add the content "TTT"
Para3.format. characterunitfirstlineindent = 2; // you can specify the indentation of the first line.
Para3.range. Font. Bold = 5; // set the section para3 font color.
Para3.range. insertparagraphafter (); // same as above
MSWord. Table Table1 = worddoc. Tables. Add (para3.range, 8, 8, ref nothing, ref nothing); // Add a table with 8 rows and 8 columns to the document
Table1.cell (1, 1). range. Font. Color = MSWord. wdcolor. wdcolorbrown; // set the font color of the 1st rows and 1 column in the table to wdcolorbrown.
Table1.cell (1, 1). range. Text = "Search for Satan"; // add content to the first class of the table in the first row.
Table1.range. paragraphformat. Alignment = MSWord. wdparagraphalignment. wdalignparagraphright; // set the content alignment in the table
Table1.cell (). Merge (table1.cell (); // combine three rows and three columns until eight rows and three columns into one column.
Object ROW = 4;
Object Col = 4;
Table1.cell (3, 1). Split (ref row, ref col); // separate three rows and one column into four rows and four columns
String picturefile = "F: \ All photos \ My photos \ aa.jpg"; // set the image path
Object linktofile = false;
Object linktodoctument = true;
Object X1 = (object) (para2.range. Start );
Object X2 = (object) (para2.range. End );
Object RR1 = worddoc. Range (ref X1, ref x2 );
Worddoc. application. activedocument. inlineshapes. addpicture (picturefile, ref linktofile, ref linktodoctument, ref RR1); // Add the image to the document
// Set the shape of the image to variable
MSWord. Shape shape1 = worddoc. application. activedocument. inlineshapes [1]. converttoshape ();
Shape1.wrapformat. type = MSWord. wdwraptype. wdwrapsquare;
// Save the worddoc
Worddoc. saveas (ref filename, ref nothing,
Ref nothing, ref nothing,
Ref nothing, ref nothing );
// Release the memory
Worddoc. Close (ref nothing, ref nothing, ref nothing );
Wordapp. Quit (ref nothing, ref nothing, ref nothing );
}
}
}