none
Remove cell borders when copying MS word table and pasting to RichTextBox control in C# (using office interop) RRS feed

  • Question

  • I am having pretty much working code that opens, copy and paste MS word file content to Winforms RichTextBox control.

    The content is from MS Word Table cell and the problem is, after pasting to Winform RichTextBox control, it is showing cell (black color) border as well.

    Any suggestion on how to remove border details and get only the richtext from Table cell. Constraint is, I am not allowed to modify word document.

    using word = Microsoft.Office.Interop.Word;
    
                var wordApp = new word.Application();
                var doc = wordApp.Documents.Open(Application.StartupPath + "\\TheDemo.doc");
                word.Range r;
                try
                {
                    foreach (word.Row aRow in doc.Tables[1].Rows)
                        foreach (word.Cell aCell in aRow.Cells)
                        {
                            aCell.Range.Copy();
                            richTextBox1.Rtf = Clipboard.GetData(System.Windows.Forms.DataFormats.Rtf).ToString();
                            MessageBox.Show(aCell.Range.Text);
                        }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message + Environment.NewLine + ex.StackTrace);
                }
                finally
                {
                    doc.Close();
                    wordApp.Quit();
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordApp);
                }

    Thanks!


    Blog: www.PavanGayakwad.blogspot.com Website: www.SrushtiSoft.com


    Saturday, March 23, 2013 4:37 AM

Answers

  • Hi PavanGayakwad,

    A workaround is to paste the cell content to a temp range without the borders and then copy the content from the temp range to the rich text box.

                    object start = doc.Tables[1].Range.End;
                    word.Range rng = doc.Range(ref start);// temp range.
                           
                    foreach (word.Row aRow in doc.Tables[1].Rows)
                        foreach (word.Cell aCell in aRow.Cells)
                        {
                            aCell.Range.Copy();
                            word.ParagraphFormat temp = aCell.Range.ParagraphFormat;
                            rng.PasteAndFormat(word.WdRecoveryType.wdSingleCellText); // copy without borders.
                            rng.Copy();
                            // or clear the content before paste
                            // richTextBox1.Clear();
                            richTextBox1.Paste();
                            
                        }
                    rng.Delete(); // delete the temp range.

    Good day.


    Yoyo Jiang[MSFT]
    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, March 25, 2013 1:14 PM
    Moderator