• Help with table (Word 2003)

    • This topic has 13 replies, 4 voices, and was last updated 20 years ago.
    Author
    Topic
    #417503

    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

    Viewing 0 reply threads
    Author
    Replies
    • #936902

      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.

      • #936903

        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

        • #936904

          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?

          • #936905

            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

            • #936910

              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?

            • #936917

              Attached is the document that contains a few tables.

              Thank you in advance for your help.

              Regards
              Rid

            • #936913

              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:

              .Columns(4).Width = 11
              

              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!

            • #936922

              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

            • #936925

              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.

            • #936930

              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

            • #936933

              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).

            • #936935

              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

            • #936936

              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.

    Viewing 0 reply threads
    Reply To: Help with table (Word 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: