How to copy rows from a table to another table in Word 2007 using interop and C#

2.4k views Asked by At

I have a master table in one word document.

First column of the table has keys. I have to create another table based upon the keys (rows) selected by the user.

The user can select a key (row) more than one time.

Table1:

TaskName    Data       Group
abc         data1      group1
pqr         data2      group2
lmn         data3      group3

TaskName column is the key column, A user can select abc,pqr,abc,pqr,lmn

This should generate a table as follows:

TaskName    Data       Group
abc         data1      group1
pqr         data2      group2
abc         data1      group1
pqr         data2      group2
lmn         data3      group3

I cannot use table.Cell().Range.Text as by doing this formatting is lost.

1

There are 1 answers

0
gizgok On BEST ANSWER

I tried first using Word.Selection, but I could not figure out a way in which I can take one row at a time and copy paste it. It might be possible to do using Word.Selection.

The next thing I thought about was copy pasting a cell at a time, but Range Property keeps a hold of table cell structure. I looked into the Word Model and tried to find out if there is some kind of termination character after every row to distinguish it as end of row. There is such a character but it's for all the cells. To find this character out, click on the office button(besides Home), click on word options, click on display,in the section always show these formatting marks on the screen, tick show all formatting marks. This displays all the non-printable characters in a word document. This is the symbol you'll be able to see ยค.

This symbol holds the structural information of the cell and the exposed ANSI characters are 13 + 7. 13 is a paragraph mark and 7 is the "end-of-cell"marker. This holds further information that points to cell structure management in the file. In Word 2007 these two characters appear as one character, so what we need to do is drop this character from the cell's Range.

I created a list to hold all the cells that I need from the master Table.

List<Microsoft.Office.Interop.Word.Cell> masterTableCells=new List<Microsoft.Office.Interop.Word.Cell>();

After that the following code removes the last character from Range

                    Microsoft.Office.Interop.Word.Table table = b.Range.Tables[1];

                table.ID = b.Name;
                for (int colCounter = 1; colCounter <= masterTable.Columns.Count; colCounter++)
                {
                    Microsoft.Office.Interop.Word.Range sourceRange = masterTable.Cell(1, colCounter).Range;
                    Microsoft.Office.Interop.Word.Range targetRange = table.Cell(1, colCounter).Range;
                    object oCharacter = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;
                    object negOne = -1;
                    sourceRange.MoveEnd(oCharacter, negOne);
                    targetRange.MoveEnd(oCharacter, negOne);
                    targetRange.FormattedText = sourceRange.FormattedText;

                }

The purpose was to obtain formatted text and by removing the character from each cell Range , we are able to point to only that particular cell's structure and not the whole tables.

If you try to run a loop with only formatted text property, you will get a table corrupt error. Hope this helps.