Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I`m struggling to make this work, but till now i didn't found any answers on the web. Here is my problem, i have a doc file created on office 2003 and i have a table in it ( header and a first empty row ). How can i select that table from C# and add a new row in it? [ I populate the table from a listview ].

Please help me.
Regards.

P.S.
Here is my code:
C#
Word.ApplicationClass myWordApp = new Word.ApplicationClass(); // our app
            Word.Document myWordDoc = new Word.Document(); // our doc

            object nothing = System.Reflection.Missing.Value; // void value
            object filename = @"D:\LeadTemplate.doc"; // template
            object notTrue = false; // our boolean false;

            myWordApp.Visible = false; 
            myWordDoc = myWordApp.Documents.Add(ref filename, ref nothing, ref nothing, ref nothing);

            // Company Name
            object oBookmark = "CNameBookMark";
            myWordDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = comboBox1.Text.ToString();
            // Invoice Date Generation
            DateTime dtToday = DateTime.Now;
            string dateN = dtToday.ToString("dd/MM/yyyy", CultureInfo.CreateSpecificCulture("en-US"));
            object dBookmark = "CurrentDateBookmark";
            myWordDoc.Bookmarks.get_Item(ref dBookmark).Range.Text = dateN.ToString();

object dest = @"D:\qfspinv"+InvoiceNumber+" Lead Statement "+comboBox1.Text.ToString()+".doc";

            myWordDoc.SaveAs(ref dest, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
            myWordApp.Application.Quit(ref notTrue, ref nothing, ref nothing);
Posted
Updated 5-Aug-12 12:49pm
v2
Comments
Kenneth Haugland 5-Aug-12 18:49pm    
Yes you can :)
Rock3rRullz 5-Aug-12 18:51pm    
How can i do that ?!

1 solution

Here is a link to create a table from scrach:
http://msdn.microsoft.com/en-us/library/office/aa192483%28v=office.11%29.aspx[^]

And if you want to find an excisting table you simply write myWordDoc.Tables(1).Cell(1,1).Value ="blablabla" and you have it. Remember that word and excel indexes start at 1 not 0. So that was that :)

[Edit]You may have to use brackes [] on the tables insted if () look Here[^][/Edit]

Easy, I hope ;)
 
Share this answer
 
v3
Comments
Rock3rRullz 6-Aug-12 2:35am    
I was looking at that link but didn't notice anything about how to find an existing table from a template. The code you provided has an error, the member cell(1,1) doesn't have a member called value.
Rock3rRullz 6-Aug-12 5:51am    
The actual syntax was:
myWordDoc.Tables[1].Cell(1, 1).Range.Text = "Text";

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900