/*  
   insert a table to document in Word
   taken from Example02.cs
*/ 
private void Example02Main()
{
   // start word and turn off msg boxes
   Word.Application wordApplication = new Word.Application();
   wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;

   // add a new document
   Word.Document newDocument = wordApplication.Documents.Add();

   // add a table
   Word.Table table = newDocument.Tables.Add(wordApplication.Selection.Range, 3, 2);

   // insert some text into the cells
   table.Cell(1, 1).Select();
   wordApplication.Selection.TypeText("This");

   table.Cell(1, 2).Select();
   wordApplication.Selection.TypeText("table");

   table.Cell(2, 1).Select();
   wordApplication.Selection.TypeText("was");

   table.Cell(2, 2).Select();
   wordApplication.Selection.TypeText("created");

   table.Cell(3, 1).Select();
   wordApplication.Selection.TypeText("by");

   table.Cell(3, 2).Select();
   wordApplication.Selection.TypeText("NetOffice");

   // save the document
   string fileExtension = GetDefaultExtension(wordApplication);
   object documentFile = 
string.Format("{0}\\Example02{1}", Application.StartupPath, fileExtension); newDocument.SaveAs(documentFile); // close word and dispose reference wordApplication.Quit(); wordApplication.Dispose(); FinishDialog fDialog = new FinishDialog("Document saved.", documentFile.ToString()); fDialog.ShowDialog(this); } #region Helper /// <summary> /// returns the valid file extension for the instance. for example ".doc" or ".docx" /// </summary> /// <param name="application">the instance</param> /// <returns>the extension</returns> private static string GetDefaultExtension(Word.Application application) { double version = Convert.ToDouble(application.Version, CultureInfo.InvariantCulture)); if (version >= 12.00) return ".docx"; else return ".doc"; } #endregion

Last edited Jun 13, 2012 at 7:40 AM by SebastianDotNet, version 5