none
WPF MVVM C# RRS feed

  • Question

  • 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(nullnull).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[i].Cells[2].Range.Text = "A-" + (i - 1).ToString() + "-" + "1";
                        oWordDoc.Tables[2].Rows[i].Cells[4].Range.Text = "A-" + (i - 1).ToString() + "-" + "2";
                   }
                    for (int i = 2; i < 12; i++)
                    {
                        oWordDoc.Tables[2].Rows[i].Cells[6].Range.Text = "S-" + (i - 1).ToString() + "-" + "3";
                        oWordDoc.Tables[2].Rows[i].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[i].Range.Text = "B-" + i.ToString();
                }
                for (int i = 1; i < 7; i++)
                {
                    oWordDoc.Tables[3].Rows[3].Cells[i].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();
              
            }

     
    • Moved by Barry Wang Monday, June 10, 2013 8:30 AM
    Saturday, June 8, 2013 11:42 AM

All replies

  • Hi

    Your issue sounds more related to Word Dev so I'll move this issue.

    Regards,


    Barry Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 10, 2013 8:30 AM
  • Please indicate the line of code that's throwing the error and include the full text of the error message.

    As far as I can see, the iteration through the data is not referring to any bookmarks. It appears to be generating tables rows as required and writing the data - no calls to any bookmarks...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 11, 2013 7:02 PM
    Moderator