Unable to set the desired column width for a table with merged cells

J

Jewel

Hi All,
I need to copy a table, the values of which are much easier computed in EXCEL; to word. I need an exact replica of the EXCEL table (i.e. I need exactly the same layout, merged cells, row heights, column widths etc...) on a word document.

GIVEN:
(just in case this info may be relevant)
The table always has 38 rows and 7 columns.
Certain columns are merged. (For E.g. in Row 1, all 7 columns are merged; in row 3, column 2 to 7 are merged).
There is no merging of rows though.

PROBLEM:
When I copy the table/range from EXCEL worksheet & paste it on the word document all the formatting (i.e. text color, font, cell alignment, merged cells) is preserved but the column widths are not. So now I need to re-size mycolumns to the original values.

Now when I use the TABLE PROPERTIES >> COLUMN TAB >> Check Preferred Width and specify the width, it works just fine. I get the exact replica table after I have specified the desired width for Column 1 to Column 7. However, arecorded Macro of the same action doesn't work; and prompts a Run-time error '5992' - "Cannot access individual columns in the this collection because the table has mixed cell widths."

Here is the RECORDED CODE

Sub Macro1()
'
' Macro1 Macro
'
'

Selection.Tables(1).Columns(1).PreferredWidthType = wdPreferredWidthPoints
Selection.Tables(1).Columns(1).PreferredWidth = CentimetersToPoints(0.79)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = CentimetersToPoints(3.49)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = CentimetersToPoints(3.49)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = CentimetersToPoints(2.99)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = CentimetersToPoints(2.7)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = CentimetersToPoints(2.7)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = CentimetersToPoints(0.79)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
End Sub


Also, I saw an example on the web & tried the following code (after modifying for the column width values)

Sub Test699()
Dim oRow As Row
Dim oTbl As Table
Set oTbl = ThisDocument.Tables(1)
With oTbl
For Each oRow In oTbl.Range.Rows
oRow.Cells(1).Width = CentimetersToPoints(0.79)
oRow.Cells(2).Width = CentimetersToPoints(3.49)** highlights on error
oRow.Cells(3).Width = CentimetersToPoints(3.49)
oRow.Cells(4).Width = CentimetersToPoints(2.99)
oRow.Cells(5).Width = CentimetersToPoints(2.7)
oRow.Cells(6).Width = CentimetersToPoints(2.7)
oRow.Cells(7).Width = CentimetersToPoints(0.79)
Next
End With
End Sub

but now I receive Run-time error '5941' - "The requested member of the collection does not exist." (The row highlighted by DEBUG is marked above).

I understand this error as in Row 1 all cells are merged, so there would beno Cell(1,2).

So how do I assign these specific values in CM for the 7 column of the table? Please help.

Thanks in advance.
Jewel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top