Hide Table/Show Table macro

N

newbei

Hi,



I have a large word document, which gets a little bit difficult to
read.

It has large amounts of tables with text mentioned below in the table.

I want to keep the tables in hide mode, when the user clicks on a
button to show the content of the table , i want the table to be
displayed

It would be easier for the readers to browse the information also.

How can i do this? it will be great if you could help

Thanking you in anticipation

Regards,
Rajesh
 
D

Doug Robbins - Word MVP

If you select the table and insert a bookmark for it - I used Table as the
name of the bookmark, you can use the following code in a macro to hide it

ActiveDocument.Bookmarks("Table").Range.Font.Hidden = True

you would use

ActiveDocument.Bookmarks("Table").Range.Font.Hidden = False

to display it.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

newbei

Doug,

Thanks a ton for taking out the time to reply to my query.
Forgive my ignorance, but I have no idea about how to create a macro
or install it.
Would it possible for you to share a template file with the
implementation or tell me the procedure to do so?

Rajesh
 
S

StevenM

How to Install a Macro
The shortcut key Alt-F11 will take you to the VBA editor.
The shortcut key Ctrl-R will open the Project Explorer (if it is not already
open).
Click on the template where you would like to store your macro, for example
click on “Normal.â€
Insert a new Module with: Insert > Module.
The code window has General & Declarations at the top.
Cut and past your macro into the code window.
You can store more than one macro in a module.
You can re-name your module by clicking on F4, this should bring up the
Properties Window. In the field following “(name)†you can change the
module’s name.
To run your macro, go back to your document and click on Alt-F8.
Alt-F8 will bring up the macro menu from which you can pick your macro and
run it.

Steven Craig Miller
 
S

StevenM

Doug,

I've always wanted to do this. Using bookmarks works great, although in my
opinion, it worked best if I selected (and so included) the blank line before
and after the table.

Steven Craig Miller
 
S

StevenM

Also, you could use this macro:

'
' Hide/Unhide Table
' Run this macro once to hide the table,
' Run it again to unhide the table.
'
Sub HideTable()
Dim sBookmark As String

sBookmark = "Table"

If ActiveDocument.Bookmarks.Exists(sBookmark) = False Then
MsgBox "The Bookmark " & Chr(34) & sBookmark & Chr(34) & " does not
exit."
Exit Sub
End If

With ActiveDocument.Bookmarks(sBookmark).Range.Font
If .Hidden = True Then
.Hidden = False
Else
.Hidden = True
End If
End With
End Sub
 
D

Doug Robbins - Word MVP

That's assuming that there is an empty paragraph before and after.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

newbei

Hi Steven,

Thanks a ton for sharing the code.
However i found a limitation of the code or may be i am doing
something wrong.

I found that if the table has more than 7 rows this macro doesnt
unhide the table back.

Could you confirm if this is the real reason or i am doing something
wrong.

Rajesh
 
J

Jay Freedman

Hi newbei,

There is no limit on the number of rows the macro can handle. What I suspect is
happening is that you added more rows to the bottom of the table, and they're
outside the bookmark. The macro hides and unhides only the part of the table
that is inside the bookmark.

Try selecting the whole table and inserting the "Table" bookmark again, and then
run the macro.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
D

Doug Robbins - Word MVP

The following code does not required that the whole of the table be
included in the bookmark

Dim sBookmark As String
sBookmark = "Table"
If ActiveDocument.Bookmarks.Exists(sBookmark) = False Then
MsgBox "The Bookmark " & Chr(34) & sBookmark & Chr(34) & " does not
exist."
Exit Sub
End If
With ActiveDocument.Bookmarks(sBookmark).Range.Tables(1).Range.Font
If .Hidden = True Then
.Hidden = False
Else
.Hidden = True
End If
End With

However, you indicated that the problem was that you could not unhide all of
the table. Did it all get hidden in the first place?
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

newbei

Doug,

Thanks for your reply.
However, you indicated that the problem was that you could not unhide all of
the table. Did it all get hidden in the first place?

Yep , this is exactly the problem.The table gets hidden upon pressing
the hide button but doesnt unhide at all once teh row count is more
than 7 .

What could be the problem?

Rajesh
 
N

newbei

Doug,

Thanks for your response.
However, you indicated that the problem was that you could not unhide all of
the table. Did it all get hidden in the first place?

Exactly, this is the problem . I can still hide the table using the
macro but it doesnt unhide if the row count is more than 7 .

Alos when i press on hide , i want th ebutton text to show"Show" , how
would that be possible.

Thanks a lot for your help

Regards,

Rajesh
 
D

Doug Robbins - Word MVP

With the table visible, first run a macro containing the following code:

ActiveDocument.Variables("hidden") = False

to set a flag that can be referred to by a macro containing the following
code that will do the hiding/unhiding as required

Dim sBookmark As String
sBookmark = "Table"
If ActiveDocument.Bookmarks.Exists(sBookmark) = False Then
MsgBox "The Bookmark " & Chr(34) & sBookmark & Chr(34) & " does not
exist."
Exit Sub
End If
With ActiveDocument
If .Variables("hidden") = True Then
.Bookmarks(sBookmark).Range.Tables(1).Range.Font.Hidden = False
.Variables("hidden") = False
Else
.Bookmarks(sBookmark).Range.Tables(1).Range.Font.Hidden = True
.Variables("hidden") = True
End If
End With



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

newbei

test
The following code does not required that the whole of the table be
included in the bookmark

Dim sBookmark As String
sBookmark = "Table"
If ActiveDocument.Bookmarks.Exists(sBookmark) = False Then
MsgBox "The Bookmark " & Chr(34) & sBookmark & Chr(34) & " does not
exist."
Exit Sub
End If
With ActiveDocument.Bookmarks(sBookmark).Range.Tables(1).Range.Font
If .Hidden = True Then
.Hidden = False
Else
.Hidden = True
End If
End With

However, you indicated that the problem was that you could not unhide all of
the table. Did it all get hidden in the first place?
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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