using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Word; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = "//endofdoc"; /* /endofdoc is a predefined bookmark */ //Start Word and create a new document. Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); //Paragraph Word.Paragraph p5; object range = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; p5 = oDoc.Content.Paragraphs.Add(ref range); p5.Range.InsertParagraphBefore(); p5.Range.Text = "Paragraph"; p5.Format.SpaceAfter = 24; p5.OutlineLevel = WdOutlineLevel.wdOutlineLevel1; p5.Range.Font.Bold = 1; p5.Range.Font.Name = "隶书"; p5.Range.Font.Size = 24; p5.Range.InsertParagraphAfter(); //Insert a paragraph at the beginning of the document. Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "Heading 1"; oPara1.Range.Font.Bold = 1; p5.OutlineLevel = WdOutlineLevel.wdOutlineLevel2; oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph. oPara1.Range.InsertParagraphAfter(); //Insert a paragraph at the end of the document. Word.Paragraph oPara2; object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; oPara2 = oDoc.Content.Paragraphs.Add(ref oRng); oPara2.Range.Text = "Heading 2"; p5.OutlineLevel = WdOutlineLevel.wdOutlineLevel2; oPara2.Format.SpaceAfter = 6; oPara2.Range.InsertParagraphAfter(); //Insert another paragraph. Word.Paragraph oPara3; oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; oPara3 = oDoc.Content.Paragraphs.Add(ref oRng); oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:"; p5.OutlineLevel = WdOutlineLevel.wdOutlineLevelBodyText; oPara3.Range.Font.Bold = 0; oPara3.Format.SpaceAfter = 24; oPara3.Range.InsertParagraphAfter(); //Insert a 3 x 5 table, fill it with data, and make the first row //bold and italic. Word.Table oTable; Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing); oTable.Range.ParagraphFormat.SpaceAfter = 6; int r, c; string strText; for (r = 1; r <= 3; r++) for (c = 1; c <= 5; c++) { strText = "r" + r + "c" + c; oTable.Cell(r, c).Range.Text = strText; } oTable.Rows[1].Range.Font.Bold = 1; oTable.Rows[1].Range.Font.Italic = 1; //Add some text after the table. Word.Paragraph oPara4; oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; oPara4 = oDoc.Content.Paragraphs.Add(ref oRng); oPara4.Range.InsertParagraphBefore(); p5.OutlineLevel = WdOutlineLevel.wdOutlineLevelBodyText; oPara4.Range.Text = "And here's another table:"; oPara4.Format.SpaceAfter = 24; oPara4.Range.InsertParagraphAfter(); oWord.Selection.TypeParagraph(); //Insert a 5 x 4 table, fill it with data, and change the column widths. wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; wrdRng.Font.Size = 12; oTable = oDoc.Tables.Add(wrdRng, 5, 4, ref oMissing, ref oMissing); oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; oTable.Columns.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle; oTable.Range.ParagraphFormat.SpaceAfter = 6; for (r = 1; r <= 5; r++) for (c = 1; c <= 4; c++) { strText = "r" + r + "c" + c; oTable.Cell(r, c).Range.Text = strText; } //Diagonal oTable.Cell(4, 1).Borders[Word.WdBorderType.wdBorderDiagonalUp].LineStyle = Word.WdLineStyle.wdLineStyleSingle; oTable.Columns[1].Width = oWord.InchesToPoints(1); //Change width of columns 1 & 2 oTable.Columns[2].Width = oWord.InchesToPoints(1); // Diagonal value oTable.Cell(4, 1).Select(); string str = oTable.Cell(4, 1).Range.Text; oWord.Selection.TypeParagraph(); oTable.Cell(4, 1).Range.Text = str + " test"; /*cell.Range.Select(); app.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; app.Selection.TypeText(text1); app.Selection.TypeParagraph(); app.Selection.TypeText(text2); app.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;*/ //Merge oTable.Cell(1, 1).Merge(oTable.Cell(1, 2)); oTable.Cell(1, 1).Merge(oTable.Cell(1, 2)); //Merge oTable.Cell(3, 2).Merge(oTable.Cell(4, 2)); //insert image string fileName = @"C:/1.jpg"; object linkToFile = false; object saveWithDocument = true; object anchor = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; oDoc.InlineShapes.AddPicture(fileName, ref linkToFile, ref saveWithDocument, ref anchor); oDoc.InlineShapes[1].Width = 300; oDoc.InlineShapes[1].Height = 500; //Keep inserting text. When you get to 7 inches from top of the //document, insert a hard page break. object oPos; double dPos = oWord.InchesToPoints(7); oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter(); do { wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; wrdRng.ParagraphFormat.SpaceAfter = 6; wrdRng.InsertAfter("A line of text"); wrdRng.InsertParagraphAfter(); oPos = wrdRng.get_Information (Word.WdInformation.wdVerticalPositionRelativeToPage); } while (dPos >= Convert.ToDouble(oPos)); object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd; object oPageBreak = Word.WdBreakType.wdPageBreak; wrdRng.Collapse(ref oCollapseEnd); wrdRng.InsertBreak(ref oPageBreak); wrdRng.Collapse(ref oCollapseEnd); wrdRng.InsertAfter("We're now on page 2. Here's my chart:"); wrdRng.InsertParagraphAfter(); //Add text after the chart. wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; wrdRng.InsertParagraphAfter(); wrdRng.InsertAfter("THE END."); //Close this form. this.Close(); } } }