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

Discussion in 'Word VBA' 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 these specific values in CM for the 7 column of the table? 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. Roy Lasris
    Replies:
    2
    Views:
    135
    Roy Lasris
    Jan 25, 2004
  2. Tim
    Replies:
    6
    Views:
    399
    Jezebel
    Aug 18, 2005
  3. Maxer
    Replies:
    1
    Views:
    4,375
    Jean-Guy Marcil
    Apr 11, 2006
  4. Steve C

    Activate desired document

    Steve C, Jan 24, 2008, in forum: Word VBA
    Replies:
    2
    Views:
    127
    Steve C
    Jan 25, 2008
  5. Steve C
    Replies:
    2
    Views:
    128
    Steve C
    May 20, 2008
  6. andreas
    Replies:
    2
    Views:
    1,241
    andreas
    Jul 23, 2008
  7. Steve
    Replies:
    1
    Views:
    154
    Doug Robbins - Word MVP
    Feb 11, 2009
  8. andreas
    Replies:
    2
    Views:
    173
    andreas
    Feb 14, 2010
Loading...