none
Using a table style to format the last row of a table RRS feed

  • Question

  • Hi,

    I'm using Word 2007 and Visual Basic for Applications. I want to format the last row of a table using a table style, but can't accomplish the task. My sample code, located in the ThisDocument class, is:

    Sub DummyTableStyle()
    
        Const dummyStyleName = "MyTable"
    
        ' Delete the style if it already exists.
        If StyleExists(Me, dummyStyleName) Then
            Styles(dummyStyleName).Delete
        End If
    
        ' Create the style.
        Dim s As Word.Style
        Set s = Styles.Add(dummyStyleName, Word.WdStyleType.wdStyleTypeTable)
        
        ' This works.
        s.Table.Condition(Word.WdConditionCode.wdFirstRow).Shading.BackgroundPatternColorIndex = Word.WdColorIndex.wdGray25
        
        ' This doesn't work.
        s.Table.Condition(Word.WdConditionCode.wdLastRow).Shading.BackgroundPatternColorIndex = Word.WdColorIndex.wdGray25
        
        Tables(2).Style = s
        
    End Sub
    

    The function StyleExists tells me if a style exists, but since it's irrelevant to my problem, its definition doesn't appear.

    When I go to table 2, the last row isn't gray. What am I doing wrong? If I check the style manually, the total row is defined as I want, but it isn't working.

    Any help is greatly appreciated,

    Carlos Mallen

    Saturday, May 18, 2013 3:13 PM

Answers

  • Hi Carlos,

    Add the following statement to the end of your subroutine. It works in Word 2010 and I don't have handy access to a Word 2007 version to test. I do know that the command is not available in Word for Mac 2011. I'm not sure why the first row displays without having to set this Boolean value, but you have to use it for the other column and row bands.

        Selection.Tables(2).ApplyStyleLastRow = True


    Kind Regards, Rich ... http://greatcirclelearning.com


    Monday, May 20, 2013 3:06 AM

All replies

  • Hi Carlos,

    Add the following statement to the end of your subroutine. It works in Word 2010 and I don't have handy access to a Word 2007 version to test. I do know that the command is not available in Word for Mac 2011. I'm not sure why the first row displays without having to set this Boolean value, but you have to use it for the other column and row bands.

        Selection.Tables(2).ApplyStyleLastRow = True


    Kind Regards, Rich ... http://greatcirclelearning.com


    Monday, May 20, 2013 3:06 AM
  • Hi Rich,

    Thanks for your answer. I've seen now how the user can apply the format manually, which might be easier because this way I don't have to write a macro. I was worried about the code defining the style, that's all, but now I see it's OK.

    The instructions for Word 2007 are:

    1. Put the cursor in a table.
    2. Go to the Design tab.
    3. Go to the Table Style Options group.
    4. Check the Header Row and / or Total Row check boxes as desired.

    Cheers,

    Carlos Mallen

    Monday, May 20, 2013 6:15 PM