How to add more than a table to a Word file

  • Thread starter Thread starter AL3ANEED
  • Start date Start date
A

AL3ANEED

Guest
How to add more than a table to a Word file



{

Microsoft.Office.Interop.Word.Application aplication = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = new Microsoft.Office.Interop.Word.Document();
string tempFile = System.IO.Path.GetTempFileName();
var WordFail = Properties.Resources.Sa1;//
File.WriteAllBytes(tempFile, WordFail);
document = aplication.Documents.Add(Template: tempFile); //'



//------------------------- Add Tables
var fibNumbers = new List<int> { 1, 2, 3, 4};
int count = 0;
var aRange = aplication.ActiveDocument.Range();

foreach (int element in fibNumbers)
{
count++;
aplication.ActiveDocument.Tables.Add(aRange, 4,3);
aplication.ActiveDocument.Tables[element].Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
aplication.ActiveDocument.Tables[element].Borders.OutsideLineStyle = WdLineStyle.wdLineStyleThinThickThinLargeGap;
aplication.ActiveDocument.Tables[element].Cell(1, 1).Merge(aplication.ActiveDocument.Tables[element].Cell(1, 4));
aplication.ActiveDocument.Tables[element].Cell(2, 1).Merge(aplication.ActiveDocument.Tables[element].Cell(2, 4));
aplication.ActiveDocument.Tables[element].Cell(3, 1).Merge(aplication.ActiveDocument.Tables[element].Cell(3, 2));
aplication.ActiveDocument.Tables[element].Cell(3, 2).Merge(aplication.ActiveDocument.Tables[element].Cell(3, 3));
aplication.ActiveDocument.Tables[element].Rows[1].Shading.BackgroundPatternColor = WdColor.wdColorSkyBlue;
aplication.ActiveDocument.Tables[element].Rows[2].Shading.BackgroundPatternColor = WdColor.wdColorSkyBlue;

aplication.Application.ActiveDocument.Tables[1].Cell(2, 1).Select();
aplication.Selection.Text = element.ToString();
}



aplication.Visible = true;//' Open Word
aplication.Activate();
}

Table one is added Ok .
But the second table is Error.

Continue reading...
 
Back
Top