Hi,

I need to open an existing word document(*.doc) with an existing table(for example 5 columns and 1 rows).Then i want add data from another table (rows are dynamic) or database. Can you help me please i'm just began learning c#

Thanks you

Here is my code

 
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Word = Microsoft.Office.Interop.Word;


namespace AjouterTableau
{
    public partial class Form1 : Form
    {
        DataTable table = new DataTable();
        public Form1()
        {
            InitializeComponent();
            //add columns
            table.Columns.Add("Objet de la facturation", typeof(string));
            table.Columns.Add("Quantité", typeof(string));
            table.Columns.Add("PU ou Base", typeof(string));
            table.Columns.Add("Taux", typeof(string));
            table.Columns.Add("Montant", typeof(string));

            //add rows
            table.Rows.Add("Dev", 2, 100,20,1500);
            table.Rows.Add("formation", 10, 100,30,1500);
            table.Rows.Add("conseil", 3, 200,40,2000);
            table.Rows.Add("Dev", 0, 300,20,1500);
            table.Rows.Add("conseil", 0, 400,40,2000);
        }
        private void createInvoice(object fileName, object saveAs)
        {
            //Connexion to word
            Word.Application wordApp = new Word.Application();
            wordApp.Visible = true;
            object savechanges = true;
            object oEndOfDoc = "\\endofdoc";
            //create a reference for optionals parameters
            object missing = System.Reflection.Missing.Value;
            //create word document
            Word.Document wordDoc=null;
            Word.Table tables = null;
            //tester s'il s'agit d'un nouveau document ou un document existant
            if (System.IO.File.Exists((string)fileName))
            {
                    object readOnly = false;
                    object isVisible = true;
                //Open existing document
                wordDoc = wordApp.Documents.Open(ref fileName, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing);
                //active document
                wordDoc.Activate();
                int i=0;
                int j=0;
                Word.Tables table = wordDoc.Tables;
                if (table.Count>0)
                {
                    //retreive the first table of the document
                    tables = table[1];
                    for (i=1; i <tables.Rows.Count; i++)
                    {
                        for (j = 1; j <= 5; j++)
                        {
                            FindAndReplace()
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Fichier template introuvable");
                return;
            }

            //save document
            wordDoc.SaveAs(ref saveAs, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            //close document
            wordDoc.Close(ref missing, ref missing, ref missing);
            //close word
            wordApp.Quit(ref missing, ref missing, ref missing);
        }
        private void FindAndReplace(Word.Application WordApp,
                                     object findText,
                                     object replaceWithText)
        {
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundsLike = false;
            object nmatchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;
            WordApp.Selection.Find.Execute(ref findText,
                ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundsLike,
                ref nmatchAllWordForms, ref forward,
                ref wrap, ref format, ref replaceWithText,
                ref replace, ref matchKashida,
                ref matchDiacritics, ref matchAlefHamza,
                ref matchControl);
        }
        private void button1_Click(object sender, EventArgs e)
        {
           createInvoice(@"c:\temp\template2.doc", @"c:\temp\facture.doc");
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}