Counting Rows of a table with a twist

J

JuanManuel

I have a document with many tables. I want to have a button or checkbox or
radiobutton located in the upper-right cell of each table so that upon
clicking on it, all of the rows within the table hide, except for the first
row.

If the the rows are hidden, another click of the event-trigger will unhide
them.

So far, I have something like this:

Private Sub BtnHide1_Click()
Dim TableNo As Integer
TableNo = 1
Call CountTablesRowsParas(TableNo)
End Sub

'Identify which table is at stake

Sub CountTablesRowsParas(WhichTable As Integer)
Dim oDoc As Document
Dim oRange As Range
Dim nTables As Long
Dim nRows As Long

Set oDoc = ActiveDocument
Set oRange = oDoc.Range

nTables = oRange.Tables.Count
MsgBox "This document has " & nTables & " table(s)."

If nTables <= 0 Then
MsgBox ("This document does not have tables. Please use the Headline
View of Word to hide lines of text")
Exit Sub
ElseIf nTables > 0 Then
nRows = oRange.Tables(WhichTable).Rows.Count
Call HideRows(nRows)
MsgBox "Table number " & WhichTable & " has " & nRows & " row(s)."
End If

'counting the rows of the table at stake and calling the hide sub


Sub HideRows(howmany As Long)
Dim i As Long

For i = 2 To howmany
ActiveDocument.Tables(1).Rows(i).Range.Font.Hidden = True
Next
End Sub

'executing the hide procedure

What I would like to do is to modify the first sub called "Private Sub
BtnHide1_Click()" so that I don't have to write as many of these as there are
tables in the document.

Is there a way to, upon clicking the event-trigger located in the
upper-right cell of a particular table, identify which table am I on and send
this parameter to "Sub CountTablesRowsParas(WhichTable As Integer)".?

Thank you,

Juan
 
L

Lene Fredborg

I suggest another solution:
Insert a MacroButton field in the first cell (or somewhere else in the first
row).
In the example below, the field code of the MacroButton field is:

{ MacroButton ShowHideTableRows "Double-click to show or hide table rows" }

In the document, the field will show the text "Double-click to show or hide
table rows" (change it to whatever you want).

When double-clicking the field, a macro named ShowHideTableRows will run.
You will find a macro below that should do what you wish. You could create
one MacroButton field and save it as an AutoText. Then you can easily insert
the AutoText in any table and the macro will work on that table.

Preconditions: In Tools > Options > View tab, “Hidden text†and “All†must
be turned off to hide hidden text on the screen, and on the Print tab,
“Hidden text†must be deselected in order not to print hidden text. You could
extend the code to handle this. But this is also true for your current
solution.


Sub ShowHideTableRows()
'Executed by double-clicking a macrobutton field that is
'placed in the first row of a table.
'Show or hides rows 2 to end of table

Dim oTable As Table
Dim oRange As Range

'Do nothing is selection is not in table
'could happen if the MacroButton field is inserted
'outside a table
If Selection.Information(wdWithInTable) = False Then
MsgBox "The selection must be in a table.", vbOKOnly, "Show/Hide
Table Rows"
Exit Sub
End If

'Double-clicking the MacroButton field sets the selection in the table
Set oTable = Selection.Tables(1)

If oTable.Rows.Count > 1 Then
'Show rows if hidden
'define range holding all rows except row 1
Set oRange = ActiveDocument.Range _
(Start:=oTable.Range.Rows(2).Range.Start, _
End:=oTable.Range.End)
If oRange.Font.Hidden = True Then
oRange.Font.Hidden = False
Else
'Hide rows if shown
oRange.Font.Hidden = True
End If
End If

'Clean up
Set oTable = Nothing
Set oRange = Nothing

End Sub

For information on MacroButton fields, see:
http://www.word.mvps.org/FAQs/TblsFldsFms/UsingMacroButton.htm

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

JuanManuel

Lene,

Thank you very much for your help. I love the whole idea behind your code,
and it is working perfectly to hide the rows of the table. Unfortunately,
upon double clicking the MacroButton Field, it isn't showing the rows again.

What could be going on?

Thanks
 
J

JuanManuel

Lene,
I actually created a new document, threw some tables in there and added your
code. It works!! It both hides and unhides rows... The weird thing is that it
does not work for the tables in my document. As I told you in the previous
reply, it only hides rows but doesn't unhide them back in there.

Could I send you the document in order to crack the mystery?

Thanks,

Juan
 
J

Jean-Guy Marcil

JuanManuel said:
Lene,

Thank you very much for your help. I love the whole idea behind your code,
and it is working perfectly to hide the rows of the table. Unfortunately,
upon double clicking the MacroButton Field, it isn't showing the rows again.

What could be going on?

The way the Range is set in the code, it always returns "wdUndefined" as a
Font.Hidden value, or 99999.
This means that the second time around, .Hidden is neither True or False, so
the first condition of the If block never quicks in. In fact, ".Hidden" is
always "wdUndefned," but the first time around, it does not matter becasue it
is not equal to True, so the Else code quicks in, as it always does
thereafter.

Try this modification:

Dim i As Long

If oTable.Rows.Count > 1 Then
'Show rows if hidden
'define range holding all rows except row 1
For i = 2 To oTable.Rows.Count
Set oRange = oTable.Rows(i).Range
oRange.Font.Hidden = Not oRange.Font.Hidden
Next
End If
 
L

Lene Fredborg

Juan,
I tested my code before posting and it worked for me each time. I just
tested it again and it still works. When I check by stepping through the
code, .Hidden always returns True or False.

Did you try using the version posted by Jean_Guy? And if you did, does it
make any difference?

Has your options for showing hidden text somehow been changed? (see my first
post).
Try to select all text and set it to _not_ hidden. Then try the macrobuttons
again. Does it make any difference?

If not, you are welcome to send me the document and I will have a look. You
can extract the e-mail address from my profile.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

JuanManuel

Hi Lene,
Yes, actually Jean_Guy's modification solved the problem. Now though, I'm
stuck in something that should be straightforward.

Now that I have all the Macrobutton Fields in my document, I'd like them to
look like an actual button. I already read the info in the tutorials where it
even shows how the Macrobutton field should look like after formatting it.

Well, I have not been able to make it look that way. In fact I have not been
able to add a background to the Macrobutton field. Whenever I try Format >
Background, it actually changes the background of the whole cell.

Any suggestions?
 
L

Lene Fredborg

You can apply a color as shading. Select the MacroButton field. Then select
Format > Borders and Shading > Shading tab, select a color and make sure to
select â€Text†in the â€Apply to†field. Click OK.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

JuanManuel

Hi Lene,

My project is working almost perfectly. The thing though, is that it only
works completely in about 80% of the tables in the document.

By completely I mean that it does work on all the tables, except for the
part where it is supposed to format both the Macrobutton field and the cell
to the left in a particular way depending on whether the table rows are
hidden or not. This part only works in about 80% of the tables.

And I don't know why. I don't see any difference between the tables for
which the code is working completely and the ones for which it is not.

Here is the code that I'm using (thanks to you and Jean-Guy):

Sub ShowHideTableRows_line2()
'Executed by double-clicking a macrobutton field that is
'placed in the first row of a table.
'Show or hides rows 2 to end of table

Dim oTable As Table
Dim oRange As Range
Dim oRowIndex As Integer

'Do nothing is selection is not in table
'could happen if the MacroButton field is inserted
'outside a table

If Selection.Information(wdWithInTable) = False Then
MsgBox "The selection must be in a table.", vbOKOnly, "Show/Hide
Table Rows"
Exit Sub
End If

'Double-clicking the MacroButton field sets the selection in the table

Set oTable = Selection.Tables(1)
oRowIndex = Selection.Rows(1).Index

Dim i As Long

'Error checking to make sure we are dealing with an actual table

If oTable.Rows.Count > 1 Then

'Show rows if hidden
'define range holding all rows except row 1

For i = oRowIndex + 1 To oTable.Rows.Count
Set oRange = oTable.Rows(i).Range
oRange.Font.Hidden = Not oRange.Font.Hidden
Next


'Formats both the Macrobutton field and the cell to the left in a
particular way
'depending on whether the table rows are hidden or not.

'=== THIS IS THE PROBLEMATIC PART OF THE CODE ==========

With oTable.Range.Font
If .Hidden = False Then
Selection.Font.Shading.BackgroundPatternColor = wdColorGreen
Selection.Font.Color = wdColorWhite
Selection.Cells(1).Previous.Range.Text = "-"

Else:
Selection.Font.Shading.BackgroundPatternColor = wdColorRed
Selection.Font.Color = wdColorAutomatic
Selection.Cells(1).Previous.Range.Text = "+"

End If
End With
End If


'Clean up
Set oTable = Nothing
Set oRange = Nothing

End Sub

Any ideas?

Thank you.
 
L

Lene Fredborg

You do not tell exactly how it goes wrong when it does not work. However,
your code line “With oTable.Range.Font†means that you check the entire table
for .font.hidden. This will never become true because the first row is never
hidden.

Does it work if you replace your problematic part with the following code?

With Selection
'If the first cell in the second row in the actual table
'is not hidden then apply green shading to the field
'else apply red shading
If .Range.Tables(1).Rows(2).Cells(1).Range.Font.Hidden = False
Then
.Font.Shading.BackgroundPatternColor = wdColorGreen
.Font.Color = wdColorWhite
.Cells(1).Previous.Range.Text = "-"
Else
.Font.Shading.BackgroundPatternColor = wdColorRed
.Font.Color = wdColorAutomatic
.Cells(1).Previous.Range.Text = "+"
End If
End With

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

JuanManuel

Hi Lene,

Now that everything is working smoothly thanks to you I need another push
for a new challenge.

I’m trying to implement something similar to AutoFilter in Excel. Two of the
columns in each table are called “Service Description†and “Type of
inspection†respectively. Since there are only three Types of inspection, a
simplified version of those two columns could look as follows:

“Service Description†“Type of Inspectionâ€
Description1 C1
Description2 C1
Description3 HGP
Description4 HGP
Description5 MI
Description6 MI

Let's suppose that every table has something similar to what's shown above.
Thanks to your help, I already have macrobutton fields in certain rows of
each table that hide/unhide the remaining rows below it. (i.e., below the row
at which the macrobutton field is positioned).

What I would like to accomplish now is some sort of selective hiding or
filtering. For example, let's suppose that I have three radiobuttons at the
very first page of the document called "CI", "HGP" and "MI" respectively.
Let's also suppose that at the time of clicking one of the hide/unhide
macrobutton fields, the "CI" radiobutton is selected.

Let's assume that the table has all of its rows hidden before the
macrobutton field is double-clicked. Once it gets, double-clicked and since
the "CI" radiobutton is the one selected, I'd like the table to show the
following:

“Service Description†“Type of Inspectionâ€
Description1 C1
Description2 C1


I was thinking about having three radiobuttons, called "CI", "HGP" and
"MI", so that upon double-clicking "MI" for example, every table in the
document hides/unhides the appropriate rows. (i.e it only shows the rows that
correspond to a CI Inpection Type).

Is there a way to accomplish this? How can I select the appropriate range to
hide it
based on the selected radiobutton?

Thank you,

Juan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top