PDA

View Full Version : Toggle button to hide rows in two different tables



Shimarisu
11-10-2014, 05:26 AM
Hello. I am quite new to VBA and would really appreciate some help.

I have successfully added a toggle button which shows or hides a table table row when it is clicked.
The trouble I'm having is that I need it to do the same thing in a different table at the same time.
ie. one button click hides a row in table 8 and also in table 10.

I am using Word 2010. This is my code so far:
Private Sub ToggleButton1_Click()
With ActiveDocument.Tables(8).Rows(2)
If .HeightRule = wdRowHeightExactly Then
.HeightRule = wdRowHeightAuto
ToggleButton1.Caption = "Yes"
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
Else
ToggleButton1.Caption = "No"
.HeightRule = wdRowHeightExactly
.Height = ".5"
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
End If
End With
lbl_Exit:
Exit Sub
End Sub

Thanks in advance for your time.

macropod
11-10-2014, 05:59 PM
At its simplest, you simply replicate the code you have for Table 8 and point it to Table 10:

Private Sub ToggleButton1_Click()
With ActiveDocument.Tables(8).Rows(2)
If .HeightRule = wdRowHeightExactly Then
.HeightRule = wdRowHeightAuto
ToggleButton1.Caption = "Yes"
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
Else
ToggleButton1.Caption = "No"
.HeightRule = wdRowHeightExactly
.Height = ".5"
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
End If
End With
With ActiveDocument.Tables(10).Rows(2)
If .HeightRule = wdRowHeightExactly Then
.HeightRule = wdRowHeightAuto
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
Else
.HeightRule = wdRowHeightExactly
.Height = ".5"
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
End If
End With
End Sub
PS: When posting code, please use the code tags, indicated by the # symbol on the posting menu.