The following code inserts a table to the Word document:
using Word = Microsoft.Office.Interop.Word;
...
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc";
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Word.Table oTable;
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
int r, c;
string strText;
for (r = 1; r <= 3; r++)
for (c = 1; c <= 5; c++)
{
strText = "r" + r + "c" + c;
oTable.Cell(r, c).Range.Text = strText;
}
This code is from the article: http://support.microsoft.com/Default.aspx?scid=kb;en-us;316384&spid=2530&sid=47
Result is:
r1c1 r1c2 r1c3 r1c4 r1c5 r2c1 r2c2 r2c3 r2c4 r2c5 r3c1 r3c2 r3c3 r3c4 r3c5
Resulting table doesn't have borders. How can I change this code to get a table with borders, like in "Insert Table" Word command?
Try this variant:
Put in cycle.
Also you can set line width and color:
You can try to use this commands:
Use them in cycle. After putting the text.
I used the following options to set all borders and it is quite easier than settings borders for every cell. Please note that the logic for getting
Range rng
is for VSTO add-in, but the table logic remains same.Result will be like table below with borders
Hope this helps.