Click here to Skip to main content
12,512,515 members (62,946 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked

Adding Data to Word Doc

, 15 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
How to add data to an existing word doc from .net

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.


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;

                //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)

                    wordApp = new Word.Application();
                    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;
                            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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sanat Palia
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionFaceing Issue???? Pin
iamanoop13-Jul-13 19:59
memberiamanoop13-Jul-13 19:59 
GeneralMy vote of 2 Pin
buyong28-Feb-12 14:36
memberbuyong28-Feb-12 14:36 
QuestionHow to... Pin
93Current22-May-09 2:31
member93Current22-May-09 2:31 
QuestionAnd what is this code suppose to do? Pin
leppie15-Feb-08 5:27
member leppie 15-Feb-08 5:27 
AnswerRe: And what is this code suppose to do? Pin
Sanat Palia19-Feb-08 3:45
memberSanat Palia19-Feb-08 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 15 Feb 2008
Article Copyright 2008 by Sanat Palia
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid