Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Article

Adding Data to Word Doc

Rate me:
Please Sign up or sign in to vote.
2.62/5 (4 votes)
15 Feb 2008CPOL 24.1K   16   5
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.

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.
        <p></p>

License

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


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFaceing Issue???? Pin
iamanoop13-Jul-13 19:59
iamanoop13-Jul-13 19:59 
GeneralMy vote of 2 Pin
buyong28-Feb-12 14:36
buyong28-Feb-12 14:36 
QuestionHow to... Pin
93Current22-May-09 2:31
93Current22-May-09 2:31 
QuestionAnd what is this code suppose to do? Pin
leppie15-Feb-08 5:27
leppie15-Feb-08 5:27 
Sigh | :sigh:

xacc.ide - now with IronScheme support
IronScheme - 1.0 alpha 1 out now

AnswerRe: And what is this code suppose to do? Pin
Sanat Palia19-Feb-08 3:45
Sanat 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.