Copy Excel range with merged cells to Word table - is this a bug and how to resolve?

480 views Asked by At

In my Word add-in (VSTO, vb.net) I built a import from/export to Excel and merged cells is giving me grief. My current environment is Win11/64 bit Office365. I've not yet been able to test on any other environment.

The importer function basically does copy in Excel and PasteExcelTable in Word like so:

excelRange.Copy
wordRange.PasteExcelTable (false, false, false)

The problem: When the Excel range has vertically merged cells then Word after paste has included rogue cells to the right of the table as you can see in the middle column of the picture below (1st column is the source tables from Excel, middle column the results in Word.).

I've built a little table parser routine to tell me the row/column index and width of each cell and the results are weird. See the 3rd column in the picture below. I've tested with 5 different table layouts. This behaviour also happens when I manually copy/paste.

The rogue cells show to the right of the vertically merged cells. In the 1st example, rows 3 and 4. There are also some rogue cells on rows 5 and 6. Basically these rogue cells are added from the vertically merged rows until the end of the table.

I can build a routine to correct the pasted table in Word but I am trying to understand what is happening so I can factor that into my correction routine.

Can you give me any ideas or pointers? Thanks! Comparing Excel source and Word result

1

There are 1 answers

2
enr1qo On

I have similar issue in my VSTO addin. The only thing I came up with that is counting the merged cells inside an Excel sheet and writing them to an array. Then i tried to merge cells in Word, but when you merging cell in Word, columns count have been shifted. You need to calculating shifting columns to correct merging cells.