Results 1 to 14 of 14
  1. #1
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with table (Word 2003)

    Hello Loungers,

    I have rather annoying code problems that I need help. The codes are used to format pre-inserted 4 columns tables to a standard size and format.

    Here are the codes:

    Sub FormatTable()
    If Selection.Information(wdWithInTable) = False Then
    MsgBox "Please position the insertion point in a table.", vbExclamation
    Exit Sub
    End If

    With Selection.Tables(1)
    .PreferredWidthType = wdPreferredWidthPoints
    .Rows.LeftIndent = CentimetersToPoints(0.8)
    .PreferredWidth = CentimetersToPoints(10.5)
    .TopPadding = CentimetersToPoints(0)
    .BottomPadding = CentimetersToPoints(0)
    .LeftPadding = CentimetersToPoints(0)
    .RightPadding = CentimetersToPoints(0.1)
    .Spacing = 0
    .AllowPageBreaks = True
    .AllowAutoFit = True
    End With

    Selection.Tables(1).Select
    Selection.Style = ActiveDocument.Styles("MyTableStyle")

    With Selection.Tables(1)
    .Columns(1).Width = CentimetersToPoints(7.5)
    .Columns(2).Width = CentimetersToPoints(1.7)
    .Columns(3).Width = CentimetersToPoints(0.3) ' Error here
    .Columns(4).Width = CentimetersToPoints(1)
    End With

    For j = 2 To 4 Step 2
    With Selection.Tables(1).Cell(1, j)
    With .Borders(wdBorderBottom)
    .LineStyle = wdLineStyleSingle
    .LineWidth = wdLineWidth050pt
    End With
    End With
    Next j
    End Sub

    The codes always stop at the line I indicated above, giving me error "Run-time Error '4608', Value out of range". However it occurs randomly, sometimes it happens, and sometimes it doesn't. But I notice it occurs more often on the page that have more than one tables that need to be formatted with the codes.

    Can anyone kindly help please?

    Regards
    Rid

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,808
    Thanks
    4
    Thanked 235 Times in 217 Posts

    Re: Help with table (Word 2003)

    The problem is due to the narrowness of the table column you are trying to apply. It is narrower than the cell padding.

    To fix the table so that the code will run you need to reduce the cell padding by:
    1. Click in the table
    2. Choose Table > Properties
    3. Under the Table tab, click the Options Button
    4. Set the left and right padding to something that totals less than 0.3cm

    Now try your code again.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Help with table (Word 2003)

    Thanks Andy.

    I am not sure if that is the case because I did set the left and right padding to 0 and 0.1 cm respectively before I set the column width.

    I reset the paddings with the code (included in my first posting):

    With Selection.Tables(1)
    .PreferredWidthType = wdPreferredWidthPoints
    .Rows.LeftIndent = CentimetersToPoints(0.4)
    .PreferredWidth = CentimetersToPoints(10.5)
    .TopPadding = CentimetersToPoints(0)
    .BottomPadding = CentimetersToPoints(0)
    .LeftPadding = CentimetersToPoints(0)
    .RightPadding = CentimetersToPoints(0.1)
    .Spacing = 0
    .AllowPageBreaks = True
    .AllowAutoFit = False
    End With

    Regards
    Rid

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,808
    Thanks
    4
    Thanked 235 Times in 217 Posts

    Re: Help with table (Word 2003)

    I have a feeling that the MyTableStyle has different ideas. Can you see what happens if you set the padding after applying the table style?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Help with table (Word 2003)

    MyTableStyle (a very misleading variable name, I am going to change it), is used to set the paragraph styles of the text in the table.

    The paddings of the tables ware not effected by applying the style. To be very sure I have moved the codes above the codes I posted in my second Posting. Yet, the error still occur.

    Thanks you.

    Rid

  6. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,808
    Thanks
    4
    Thanked 235 Times in 217 Posts

    Re: Help with table (Word 2003)

    I assumed that MyTableStyle was a table style not a paragraph style.

    Can you attach a file with a few empty tables and the code you are using?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  7. #7
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Help with table (Word 2003)

    As best I can tell, from some brief experimentation, is that there's a minimum column width of 11 points. From the GUI, Word enforces a slightly wider minimum of .17", as shown in the attached screenshot. But from VBA, you can make it a bit narrower, at 11 pts:
    <pre>.Columns(4).Width = 11
    </pre>


    That's 2.5 points smaller than your desired .3 cm, but I would think it's good enough for most situations.

    I'd suggest changing the problem line to code shown above, and see if that produces acceptable results.

    Hope this helps!
    Attached Images Attached Images

  8. #8
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Help with table (Word 2003)

    Attached is the document that contains a few tables.

    Thank you in advance for your help.

    Regards
    Rid
    Attached Files Attached Files

  9. #9
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Help with table (Word 2003)

    Thanks Andrew,

    I tried your suggestion, but unfortunately, it does not seem to solve the issue.

    Now, the codes generate the same error at the first line that resize the column width (instead of 3rd line), no matter how wide I set the width to (ie. no chance to get to Column 3 and resize the width).

    ...
    With Selection.Tables(1)
    .Columns(1).Width = CentimetersToPoints(7.5) ' Error here
    .Columns(2).Width = CentimetersToPoints(1.7)
    .Columns(3).Width = 15
    .Columns(4).Width = CentimetersToPoints(1)
    End With
    ....


    Regards
    Rid

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Help with table (Word 2003)

    When I run the code in the document you attached, with .Columns(3).Width = CentimetersToPoints(0.3) changed to .Columns(3).Width = 11 (or larger), it works OK on all tables.

  11. #11
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Help with table (Word 2003)

    Thanks Hans,

    I tried after closing another document that has similar code, and it works (BUT...see below). I am still trying to figure out the cause why I cannot use the same codes on my actual document.

    Now, the interesting part, can you see at your side that the width of columns in table 1 is different from table 2 (or table 3)? Do you know why?

    Regards
    Rid

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Help with table (Word 2003)

    Before or after running the macro on each of the tables? In the document as posted, the preferred width for column 3 is different (0.3 cm for Table1, 0.41 cm for Table2).

  13. #13
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Help with table (Word 2003)

    Hans,

    Thank you so much. Everything is so far so good after I changed all the .Width to .PreferredWidth (including on the main document that I am working on). What is the difference between two or rather, when should one be used instead of the other?

    Regards
    Rid

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Help with table (Word 2003)

    Width is always measured in points. How PreferredWidth is measured depends on the setting of PreferredWidthType. If this is set to wdPreferredWidthPoints, PreferredWidth is equivalent to Width, but if it is set to wdPreferredWidthPercent, PreferredWidth is a percentage of the available width - if you change the page size or page margins, the column width will be adjusted automatically.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •