Is it possible to create a macro in Word that will select and hide a specific table row when a certain checkbox is unticked.


I am a complete newbie with word and macros and that is why I am here asking my betters for help!

I have a word template that I have to regularly edit for work. It contains two tables. The first table is an index list of the contents of the second table and there are check-boxes at the end of each row.

The second table contains 50 rows of data.

I want to know if it is possible to create 50 macros (one for each checkbox) so that when a specific checkbox in the index table is un-ticked (e.g table 1, checkbox 1) the corresponding row in table 2 is automatically hidden (e.g table 2, row 1).

The ability to  un-tick a few check boxes - rather than trawling through the table, highlighting each redundant row and then manually hiding - would be amazing and really help me work more efficiently.

If it is possible, could one of you kind people please explain how to do it?

Many thanks!
Jon SelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Probably, but we might need a bit more information.

This uses legacy formfield checkboxes

Sub HideRows()
Dim tbl1 As Table
Dim tbl2 As Table
Dim rw As row
Dim rng1 As Range
Dim ffld As FormField

Set tbl1 = ActiveDocument.Tables(1)
Set tbl2 = ActiveDocument.Tables(2)

For Each rw In tbl1.Rows
    For Each ffld In rw.Range.FormFields
        If ffld.Type = wdFieldFormCheckBox Then
            tbl2.Rows(rw.Cells(1).RowIndex).Range.Font.Hidden = Not ffld.CheckBox.Value
            Exit For
        End If
    Next ffld
Next rw
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sorry. They have taken away the time that questions were posted, so I can't assume that you are still awake, but, for myself, I am now going to bed and won't be able to respond to your response soon.
Jon SelAuthor Commented:
Hi Graham,

I would like to thank you so much for taking the time to help me with this. I copied your code straight into a new macro in word and it is working!

When I un-tick a checkbox in the first table it is now hiding the corresponding row in the second table!

Oddly enough, after trying it a few times it seems to randomly give me the following error message:

Run-time error '4605':

This method or property is not available because the object refers to a protected area of the document.

However, after clicking abort the macro still hides the row so it is not a problem.

Thank you very much, much appreciated - You sir are brilliant.
Jon SelAuthor Commented:
Thank you sir for taking the time to write a custom macro for me!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.