prabhu

prabhu

  • NA
  • 64
  • 12.1k

Stuck when creating Word doc using interop dll c#

Jan 30 2019 6:51 AM
I am  getting data services layer as list it as 10 count
I looping it writing a word docment 
My expectation is one list for one page
 
but my out put  each list overwritting  next list finally only 10 th   list comes in output one page.
 
please help on this 
//code
private void CreateDocument(List orr)
{
try
{
List QRR = RTS.GETQRR_Document(orr);
Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
winword.ShowAnimation = false;
winword.Visible = true;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
// for loop for each word for each list
foreach (var Qr in QRR)
{
foreach (Microsoft.Office.Interop.Word.Section section in document.Sections)
{
Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
headerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlack;
headerRange.Font.Size = 13;
headerRange.Font.Name = "Arial";
headerRange.Text = Qr.HEADER;
}
foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
{
//Get the footer range and add the footer details.
Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
footerRange.Font.Size = 10;
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
footerRange.Text = "Footer text goes here";
}
document.Content.SetRange(0, 0);
document.Content.Text = Qr.TECH + Environment.NewLine;
Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
//object styleHeading1 = "Heading 1";
para1.Range.Text = Qr.DESCRIPTION1;
para1.Range.InsertParagraphAfter();
//Add paragraph with Heading 2 style
Microsoft.Office.Interop.Word.Paragraph para2 = document.Content.Paragraphs.Add(ref missing);
object styleHeading2 = "Heading 1";
para2.Range.set_Style(ref styleHeading2);
para2.Range.Text = Qr.FIT_RATE_CONTENT + Qr.TEMPRATURE;
para2.Range.InsertParagraphAfter();
//Create a 5X5 table and insert some dummy record
Word.Table objTable = document.Tables.Add(para1.Range, 5, 7, ref missing, ref missing);
objTable.Borders.Enable = 1;
objTable.Range.ParagraphFormat.SpaceAfter = 7;
string strText;
strText = "Failure Mechanisms";
objTable.Rows[1].Range.Text = strText;
objTable.Rows[1].Range.Font.Bold = 1;
objTable.Rows[1].Range.Font.Size = 7;
objTable.Rows[2].Range.Font.Size = 7;
objTable.Rows[3].Range.Font.Size = 7;
objTable.Rows[4].Range.Font.Size = 7;
objTable.Rows[5].Range.Font.Size = 7;
objTable.Rows[1].Range.Font.Position = 1;
objTable.Rows[1].Range.Font.Name = "Arial";
objTable.Rows[2].Range.Font.Name = "Arial";
objTable.Rows[3].Range.Font.Name = "Arial";
objTable.Rows[4].Range.Font.Name = "Arial";
objTable.Rows[5].Range.Font.Name = "Arial";
objTable.Rows[1].Cells[1].Merge(objTable.Rows[3].Cells[1]);
//objTable.Range.SetRange(1, 3);
//objTable.Cell(1,1).Merge
objTable.Cell(1, 2).Merge(objTable.Cell(1, 3));
objTable.Cell(1, 4).Merge(objTable.Cell(1, 5));
objTable.Cell(2, 4).Split(1, 5);
objTable.Cell(2, 4).Range.Text = "EAev";
objTable.Cell(2, 5).Range.Text = "TAF";
objTable.Cell(2, 6).Range.Text = "VAF";
objTable.Cell(2, 7).Range.Text = "OAF";
objTable.Cell(2, 8).Range.Text = "MTTF(Yrs)";
objTable.Cell(1, 5).Delete();
objTable.Cell(4, 7).Delete();
objTable.Cell(5, 7).Delete();
objTable.Cell(2, 11).Delete();
objTable.Cell(3, 7).Delete();
objTable.Cell(1, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objTable.Cell(1, 3).Width = 130;
objTable.Cell(4, 4).Width = 130;
objTable.Cell(5, 4).Width = 130;
objTable.Cell(4, 2).Width = 50;
objTable.Cell(4, 3).Width = 100;
objTable.Cell(5, 2).Width = 50;
objTable.Cell(5, 3).Width = 100;
objTable.Cell(4, 1).Range.Text = "";
objTable.Cell(5, 1).Range.Text = "";
objTable.Cell(2, 4).Width = 24;
objTable.Cell(2, 5).Width = 25;
objTable.Cell(2, 6).Width = 25;
objTable.Cell(2, 7).Width = 26;
objTable.Cell(2, 8).Width = 30;
objTable.Cell(4, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objTable.Cell(5, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objTable.Cell(1, 2).Range.Text = "";
objTable.Cell(2, 2).Range.Text = "";
objTable.Cell(2, 3).Range.Text = " ";
objTable.Cell(1, 3).Range.Text = "";
objTable.Cell(1, 4).Range.Text = "";
objTable.Cell(2, 9).Range.Text = "";
objTable.Cell(2, 10).Range.Text = "";
objTable.Cell(3, 2).Split(1, 2);
objTable.Cell(3, 2).Range.Text = "24";
objTable.Cell(3, 2).Next.Range.Text = "48";
objTable.Cell(3, 4).Split(1, 3);
objTable.Cell(1, 2).Width = 150;
objTable.Cell(2, 2).Width = 50;
objTable.Cell(2, 3).Width = 100;
objTable.Cell(3, 2).Width = 25;
objTable.Cell(3, 3).Width = 25;
objTable.Cell(3, 4).Width = 32;
objTable.Cell(3, 5).Width = 34;
objTable.Cell(3, 6).Width = 34;
objTable.Cell(3, 4).Range.Text = "";
objTable.Cell(3, 5).Range.Text = "";
objTable.Cell(3, 6).Range.Text = "";
objTable.Cell(3, 7).Width = 130;
objTable.Cell(1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objTable.Cell(1, 4).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
if (Qr.DR_REQ == "YES")
{
Word.Paragraph para3 = document.Content.Paragraphs.Add(ref missing);
// object styleHeading2 = "Heading 2";
// para3.Range.set_Style(ref styleHeading2);
para3.Range.Text = Qr.DESCRIPTION2;
para3.Range.InsertParagraphAfter();
Word.Table secondtab = document.Tables.Add(para2.Range, 4, 5, ref missing, ref missing);
secondtab.Borders.Enable = 1;
}
//l winword.Selection.InsertNewPage();
//Save the document
}
object filename = @"D:\temp5.docx";
document.SaveAs2(ref filename);
document.Close(ref missing, ref missing, ref missing);
document = null;
winword.Quit(ref missing, ref missing, ref missing);
winword = null;
}
catch (Exception ex)
{
}
}

Answers (1)