Adding Tables/Data to existing Word doc from .net
If you have wondered how to add Tables/Data to existing Document. Here is a small and simple code which shows you how to do it.
Background
Using the code
Add a reference to Interop.Office and Interop.Word
object missing = System.Reflection.Missing.Value;
object fileName = _filename;
object saveChanges = true;
object newTemplate = false;
object docType = 1;
object isVisible = false;
object confirmConversions = Type.Missing;
object readOnly = false;
object addToRecentFiles = Type.Missing;
object passwordDoc = Type.Missing;
object passwordTemplate = Type.Missing;
object revert = Type.Missing;
object writepwdoc = Type.Missing;
object writepwTemplate = Type.Missing;
object format = Type.Missing;
object encoding = Type.Missing;
object visible = true;
object openRepair = Type.Missing;
object docDirection = Type.Missing;
object notEncoding = Type.Missing;
object xmlTransform = Type.Missing;
int row = -1;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
Word._Application wordApp = null;
Word._Document wordDoc = null;
try
{
//Proceed only if there are some comments to be displayed
if (_CommentResultsDS != null)
{
//exit if there are no rows in CommentsResult
if (_CommentResultsDS.COMMENTS.Rows.Count == 0)
return;
wordApp = new Word.Application();
//wordApp.Version
wordApp.Visible = false;
//Open the doc
wordDoc = wordApp.Documents.Open(
ref fileName,
ref confirmConversions, ref readOnly, ref addToRecentFiles,
ref passwordDoc, ref passwordTemplate, ref revert, ref writepwdoc,
ref writepwTemplate, ref format, ref encoding, ref visible);
if (wordDoc != null)
{
//Create a table in word doc
Word.Table oTable = null;
Word.Range wrdRng = wordDoc.Bookmarks.Item(ref oEndOfDoc).Range;
if (wordDoc.Tables.Count > 0)
{
oTable = wordDoc.Tables.Item(1);
}
if (oTable == null)
{
oTable = wordDoc.Tables.Add(wrdRng, _CommentResultsDS.COMMENTS.Rows.Count + 1, 3, ref missing, ref missing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
oTable.ID = TABLE_ID;
row = 1;
//Add the header row
oTable.Cell(row, 1).Range.Text = "User Id";
oTable.Cell(row, 2).Range.Text = "Date Added";
oTable.Cell(row, 3).Range.Text = "Comments";
//Change the font and color of the header row
oTable.Rows.Item(row).Range.Font.Bold = 1;
oTable.Rows.Item(row).Range.Font.Size = 12;
oTable.Rows.Item(row).Range.Font.Color = Word.WdColor.wdColorDarkBlue;
oTable.Rows.Item(row).Range.Font.Name = TABLE_FONT_NAME;
}
else
{
row = oTable.Rows.Count;
}
//Don't show the border lines
oTable.Borders.Enable = 1;
foreach (CMPNCommentsDS.COMMENTSRow commentsRow in _CommentResultsDS.COMMENTS.Rows)
{
object beforeRow = Type.Missing;
row += 1;
oTable.Rows.Add(ref beforeRow);
oTable.Cell(row, 1).Range.Text = commentsRow.ADD_USR_ID;
oTable.Cell(row, 2).Range.Text = commentsRow.ADD_TMSTMP.ToShortDateString();
oTable.Cell(row, 3).Range.Text = commentsRow.COMMENTS;
oTable.Rows.Item(row).Range.Font.Size = 10;
oTable.Rows.Item(row).Range.Font.Color = Word.WdColor.wdColorBlue;
oTable.Rows.Item(row).Range.Font.Name = TABLE_FONT_NAME;
}
}
Remember to set the Language of your code snippet using the
Language dropdown.