EDN Admin
Well-known member
hi, I am a newbie for wpf mvvm, i want to generate a report on a button click,the report consist of two tables A and B, through bookmarks in table A i have binded the properties and its working fine but as Table B is Collection of data its giving me an exception that its not able to find corresponding bookmark,so how i can resolve it and table B can be printed multiple time as per data.
the code i am using is
string docPath = "D:\Report.docx";
object missing = System.Type.Missing;
Word.Application oWord = new Word.Application();
Word.Document oWordDoc = new Word.Document();
oWord.Visible = true;
oWordDoc = oWord.Documents.Open(docPath);//, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);
PropertyInfo[] _PropertyInfos = typeof(BookMarkProperties).GetProperties();
foreach (PropertyInfo propInfo in _PropertyInfos)
{
object oBookMark = propInfo.Name.ToString();
oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Font.Size = 5;
oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = propInfo.GetValue(null, null).ToString();
}
#region run
object oBookMark2 = "RunNumber";
oWordDoc.Bookmarks.get_Item(ref oBookMark2).Range.Font.Size = 5;
oWordDoc.Bookmarks.get_Item(ref oBookMark2).Range.Text = "1";
for (int j = 1; j < 6; j++)
{
for (int i = 2; i < 13; i++)
{
oWordDoc.Tables[2].Rows.Cells[2].Range.Text = "A-" + (i - 1).ToString() + "-" + "1";
oWordDoc.Tables[2].Rows.Cells[4].Range.Text = "A-" + (i - 1).ToString() + "-" + "2";
}
for (int i = 2; i < 12; i++)
{
oWordDoc.Tables[2].Rows.Cells[6].Range.Text = "S-" + (i - 1).ToString() + "-" + "3";
oWordDoc.Tables[2].Rows.Cells[8].Range.Text = "S-" + (i - 1).ToString() + "-" + "4";
}
}
oWordDoc.Tables[3].Rows.Add(ref missing);
for (int i = 1; i < 7; i++)
{
oWordDoc.Tables[3].Rows[2].Cells.Range.Text = "B-" + i.ToString();
}
for (int i = 1; i < 7; i++)
{
oWordDoc.Tables[3].Rows[3].Cells.Range.Text = "C-"+i.ToString();
}
oWordDoc.Tables[3].Range.Copy();
oWordDoc.Tables[3].Range.Paste();
oWordDoc.Tables[3].Rows[2].Range.Copy();
oWordDoc.Tables[3].Rows[2].Range.Paste();
Clipboard.Clear();
}
View the full article
the code i am using is
string docPath = "D:\Report.docx";
object missing = System.Type.Missing;
Word.Application oWord = new Word.Application();
Word.Document oWordDoc = new Word.Document();
oWord.Visible = true;
oWordDoc = oWord.Documents.Open(docPath);//, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);
PropertyInfo[] _PropertyInfos = typeof(BookMarkProperties).GetProperties();
foreach (PropertyInfo propInfo in _PropertyInfos)
{
object oBookMark = propInfo.Name.ToString();
oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Font.Size = 5;
oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = propInfo.GetValue(null, null).ToString();
}
#region run
object oBookMark2 = "RunNumber";
oWordDoc.Bookmarks.get_Item(ref oBookMark2).Range.Font.Size = 5;
oWordDoc.Bookmarks.get_Item(ref oBookMark2).Range.Text = "1";
for (int j = 1; j < 6; j++)
{
for (int i = 2; i < 13; i++)
{
oWordDoc.Tables[2].Rows.Cells[2].Range.Text = "A-" + (i - 1).ToString() + "-" + "1";
oWordDoc.Tables[2].Rows.Cells[4].Range.Text = "A-" + (i - 1).ToString() + "-" + "2";
}
for (int i = 2; i < 12; i++)
{
oWordDoc.Tables[2].Rows.Cells[6].Range.Text = "S-" + (i - 1).ToString() + "-" + "3";
oWordDoc.Tables[2].Rows.Cells[8].Range.Text = "S-" + (i - 1).ToString() + "-" + "4";
}
}
oWordDoc.Tables[3].Rows.Add(ref missing);
for (int i = 1; i < 7; i++)
{
oWordDoc.Tables[3].Rows[2].Cells.Range.Text = "B-" + i.ToString();
}
for (int i = 1; i < 7; i++)
{
oWordDoc.Tables[3].Rows[3].Cells.Range.Text = "C-"+i.ToString();
}
oWordDoc.Tables[3].Range.Copy();
oWordDoc.Tables[3].Range.Paste();
oWordDoc.Tables[3].Rows[2].Range.Copy();
oWordDoc.Tables[3].Rows[2].Range.Paste();
Clipboard.Clear();
}
View the full article