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

Discussion in 'Word VBA Beginners' started by Jewel, Jan 13, 2014.

  1. Jewel

    Jewel Guest

    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 specific width for the 7 columns of the table in centimeters? Please help.

    Thanks in advance.
    Jewel
     
    Jewel, Jan 13, 2014
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Ken Wan

    How to handle tables with merged cells?

    Ken Wan, Aug 1, 2003, in forum: Word VBA Beginners
    Replies:
    3
    Views:
    251
    Ken Wan
    Aug 3, 2003
  2. Brent

    "Page 1" appears in header of merged document

    Brent, Feb 29, 2004, in forum: Word VBA Beginners
    Replies:
    3
    Views:
    132
    Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT
    Mar 1, 2004
  3. Ruby Tuesday

    Merged rows and columns

    Ruby Tuesday, Mar 2, 2004, in forum: Word VBA Beginners
    Replies:
    4
    Views:
    159
    Word Heretic
    Mar 4, 2004
  4. J.Fandango

    1 filename per each Merged Letter

    J.Fandango, Jun 25, 2004, in forum: Word VBA Beginners
    Replies:
    7
    Views:
    208
    j.fandango
    Jun 28, 2004
  5. alfonso gonzales

    determine a merged cell span column value

    alfonso gonzales, Feb 13, 2005, in forum: Word VBA Beginners
    Replies:
    1
    Views:
    157
    Jezebel
    Feb 13, 2005
  6. Steve
    Replies:
    2
    Views:
    507
    Steve
    Mar 7, 2005
  7. Dig-it

    converting tables with merged rows/columns to text

    Dig-it, Aug 7, 2005, in forum: Word VBA Beginners
    Replies:
    3
    Views:
    172
    Dig-IT
    Aug 11, 2005
  8. kostaus

    Delete a Row in a Table with Merged cells

    kostaus, May 3, 2012, in forum: Word VBA Beginners
    Replies:
    0
    Views:
    455
    kostaus
    May 3, 2012
Loading...