using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Office.Core; using Microsoft.Office.Interop.Word; using System.Windows; using System.Windows.Forms; using System.Drawing; using System.IO; namespace createWordDocument { class Program { static Microsoft.Office.Interop.Word.Application word_app = null; static WdColor COLOR_WHITE = (WdColor)0xFFFFFF; static WdColor COLOR_DARK_BLUE = (WdColor)0xBD814F; static WdColor COLOR_LIGHT_BLUE = (WdColor)0xEEDFD3; static WdColor COLOR_BLACK = (WdColor)0x000000; const int NB_CHAPTERS = 10; //Microsoft.Office.Interop.Word.Font FONT = new Microsoft.Office.Interop.Word.Font("arial"); static int Main(string[] args) { List titles = new List(); List paragraphs = new List(); /* creation of NB_CHAPTERS titles and NB_CHAPTERS paragraphs */ for (int x = 0; x < NB_CHAPTERS; x++) { titles.Add(string.Format("Title {0}", x)); string text = ""; for(int y = 0; y < 100; y++) { text += string.Format("paragraph {0} ", y); } text += "\n"; paragraphs.Add(text); } /* Let's get the Word application object */ word_app = new Microsoft.Office.Interop.Word.Application(); word_app.Visible = false; /* to not open the document in Word */ /* Let's create the Word document */ object missing = System.Reflection.Missing.Value; object templateName = Path.Combine(Directory.GetCurrentDirectory(), @"template3.dotx"); //Microsoft.Office.Tools.Word.Bookmark firstParagraph; Microsoft.Office.Interop.Word._Document word_doc = word_app.Documents.Add(ref templateName, ref missing, ref missing, ref missing); /* Let's fill in the document */ for (int x = 0; x < NB_CHAPTERS; x++) { /* Adds a title */ Microsoft.Office.Interop.Word.Paragraph title = word_doc.Paragraphs.Add(); title.Range.Text = titles[x]; title.Range.set_Style("Titre 1"); /* Adds a paragraph */ Microsoft.Office.Interop.Word.Paragraph paragraph = word_doc.Paragraphs.Add(); title.Range.Text = paragraphs[x]; title.Range.set_Style("Normal"); } /* Let's clean up */ /* Let's close the brand new document and Word */ object save_changes = false; object filename = Path.Combine(Directory.GetCurrentDirectory(), @"test.doc"); word_doc.SaveAs(ref filename); word_doc.Close(ref save_changes, ref missing, ref missing); word_app.Quit(); return 0; } } }