• Missing property? (XP 2002 SP2)

    Author
    Topic
    #439008

    Hi y’all
    I have two small macros set up to format tables the way I like them. One of them (Tableformat) operates on the current table and the other one (Tablesformat) on all the tables in the document.

    So far so good.

    However, I though, why have two almost identical macros, why not have a private sub that is called by each of them and is passed a table object.

    Well, first of all I tried:

    Sub Tablesformat()
    Dim t As Table
    For Each t In ActiveDocument.Tables
      doTableformat (t)
    Next
    Exit Sub
    

    Calling:
    Private Sub doTableformat(t As Table)

    But that gave me a type mismatch error? I’d love it if someone could tell me why?

    Then I tried swapping and using a Variant.
    All seemed to work well with this, untill I reached the line:
    With .Borders(wdBorderVertical)

    When I got the error “The requested member of the collection does not exist”?

    So why does this particular attribute not exist when I call the sub with a table object, when it does exist if I just loop in the code?

    More to the point, how do I achieve the end goal of having a sub that I can call with a table object?

    Here is the doTableformat Sub:

    Private Sub doTableformat(t As Variant)
    
    With t
        With .Rows.First
          .HeadingFormat = True
          With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorAutomatic
            .BackgroundPatternColor = wdColorGray20
          End With
        End With
        .Rows.AllowBreakAcrossPages = False
        .Rows.First.Range.ParagraphFormat.KeepWithNext = True
        With .Borders(wdBorderLeft)
          .LineStyle = wdLineStyleSingle
          .LineWidth = wdLineWidth050pt
          .Color = wdColorGray50
        End With
        With .Borders(wdBorderRight)
          .LineStyle = wdLineStyleSingle
          .LineWidth = wdLineWidth050pt
          .Color = wdColorGray50
        End With
        With .Borders(wdBorderTop)
          .LineStyle = wdLineStyleSingle
          .LineWidth = wdLineWidth050pt
          .Color = wdColorGray50
        End With
        With .Borders(wdBorderBottom)
          .LineStyle = wdLineStyleSingle
          .LineWidth = wdLineWidth050pt
          .Color = wdColorGray50
        End With
        With .Borders(wdBorderHorizontal)
          .LineStyle = wdLineStyleSingle
          .LineWidth = wdLineWidth050pt
          .Color = wdColorGray50
        End With
        With .Borders(wdBorderVertical)
          .LineStyle = wdLineStyleSingle
          .LineWidth = wdLineWidth050pt
          .Color = wdColorGray50
        End With
      End With
    End Sub
    

    TIA
    Regards
    Paul

    Viewing 0 reply threads
    Author
    Replies
    • #1048059

      You can use

      Private Sub doTableformat(t As Table)

      but you must call it as

      doTableformat t

      i.e. don’t use (t) since it changes t to a ByVal argument.

      If a table has only one row, .Borders(wdBorderHorizontal) is not available, and if it has only one column, .Borders(wdBorderVertical) is not available. You can get around this to test for the number of rows/columns:

          If .Rows.Count > 1 Then
            With .Borders(wdBorderHorizontal)
              .LineStyle = wdLineStyleSingle
              .LineWidth = wdLineWidth050pt
              .Color = wdColorGray50
            End With
          End If
          If .Columns.Count > 1 Then
            With .Borders(wdBorderVertical)
              .LineStyle = wdLineStyleSingle
              .LineWidth = wdLineWidth050pt
              .Color = wdColorGray50
            End With
          End If
      

      This skips the code if it is not applicable.

      • #1048061

        Thanks Hans,
        I still wonder why it was only that one property that was missing when it was failing.

        • #1048064

          The top, left, right and bottom borders are always available, whatever the number of rows and columns is. The horizontal borders between cells are only available if there is more than one row, and similar for the vertical borders between cells.

          • #1048066

            Yeah I got that,
            but this is the same table. All the other properties seemed to get passed OK, but that one did not.

            • #1048069

              Sorry, you’ve lost me.

            • #1048141

              I don’t have a reason for the Vertical Border to fail but I would be inclined to modify the code to use a table style instead.

              In the past I have found the code to colour the table borders to be very slow. A far faster method of applying the shading and borders is to apply a table style.

            • #1048225

              I’ve never really played around with table styles. I’ll take a look, thanks.

              However, very slow is relative. The macro does 10 tables in about a second which is quick enough for me.

    Viewing 0 reply threads
    Reply To: Missing property? (XP 2002 SP2)

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

    Your information: