Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Microsoft Word Macro Question

Status
Not open for further replies.

ctsang

Technical User
Jan 27, 2003
15
US
Hi there,

Quick question, lets say I have a word document with a checkbox and a table with some text underneath it. Is there anyway to program the checkbox macro to hide the table if the checkbox is checked? If not, is there some similar way to do this in word?

Thanks!
 


Hi,

How about changing the text color to White -- kinda hides things.

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
Hm, not sure if that'll completely solve the problem but I'm curious, how do you do that?
 


Turn on your macro recorder, select the table, change the font color to white.

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
There is a MUCH better way. This is absolutely possible.

It CAN be done with a formfield checkbox, but it a lot (really a lot) easier using an ActiveX checkbox from the Controls toolbar. I just tested this, it works.

ASSUMPTION: checkbox CHECKED = HIDE table; UNCHECKED = SHOW table

Make the ActiveX Change event fire a routine that:

1. looks at the value of the checkbox

2. goes to the next table - with the assumption that IS the table you want to go.

3. selects the table

4. if checkbox is UNCHECKED, and font of table is hidden, unhide table;
if checkbox is CHECKED, and font of table is hidden, exit sub;

and vice versa.

However, you must not only make the table font hidden, but you must check and toggle the View Options as well, to make sure things are viewing properly, based on the logic of the checkbox.

Actually, ctsang...thanks. I had not tried to something like this before. I can use this for a number of things. It works great.




Gerry
 


Gerry,

I knew that you'd come up with a slick solution!

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
Thanks. I liked so much I just posted the code and the sample document at VBAExpress for a KnowledgeBase Entry. Unless I screwed it up - always possible - it should get approval fairly soon. I have just been expanding the concept, testing it on a variety of things besides a table.

Works great for most anything you can grab a range of...which is just about everything.

Gerry
 
Thanks for the input. I'll give it a shot.
 
Hi Fumei,

Would it be possible for you to post the sample code and document? I'm having trouble trying to do what you outlined in your earlier post.

Thanks.
 
What you need:

An ActiveX checkbox. NOT a formfield - although it can be done with formfields, it just takes a bit more code.

OK, so you have an ActiveX checkbox. Oh, just in case....this means a checkbox inserted into the document using the Controls toolbar. Do not forget to turn off Design mode after you insert the checkbox.

A table. The code belows actions the NEXT table after the checkbox. This can easily be adjusted for any table you like.

Put the code in the ThisDocument module. It is important that the code be in the ThisDocument, as ActiveX control event are referenced directly from there. You can call control events from other nodule, but it takes a little more syntax.

Anyway, copy the code into ThisDocument. I have it set up that CHECKING the box disappears the table; UNCHECKING makes the table be visible. You can easily reverse this if you choose.

Code:
Private Sub CheckBox1_Change()
Call ShowHideTable
End Sub

Sub ShowHideTable()
With Selection
   .GoTo What:=wdGoToTable, Which:=wdGoToNext, _
      Count:=1, Name:=""
   .Tables(1).Select
End With
If CheckBox1.Value = True Then
   Selection.Font.Hidden = True
   ActiveWindow.View.ShowAll = False
Else
   Selection.Font.Hidden = False
   ActiveWindow.View.ShowAll = True
   With Selection
      .Collapse direction:=wdCollapseStart
      .MoveLeft unit:=wdCharacter, Count:=1
   End With
End If
End Sub

NOTE:

This does NOT have full error trapping!

Remember that the above actions the NEXT table.

There are a LOT of other properties associated with ActiveWindow.View. Such as:

.ShowAnimation = True
.Draft = False
.WrapToWindow = False
.ShowPicturePlaceHolders = False
.ShowFieldCodes = False
.ShowBookmarks = False
.FieldShading = wdFieldShadingWhenSelected
.ShowTabs = False
.ShowSpaces = False
.ShowParagraphs = False
.ShowHyphens = False
.ShowHiddenText = False
.ShowAll = True
.ShowDrawings = True
.ShowObjectAnchors = False
.ShowTextBoundaries = False
.ShowHighlight = True
.DisplayPageBoundaries = True
.DisplaySmartTags = True

So if you want, say, the paragraph marks visible, but the table invisible, then you have to explicitly add .ShowParagraphs = True to the routine.

If you wanted to have a range of text toggled, then use a Range. I made a bookmark of about four pages of text, named it "mytext", and used a second checkbox (Checkbox2) to toggle the text visibility.

Code:
Private Sub CheckBox2_Change()
Call ShowHideBookmark
End Sub

Sub ShowHideBookmark()
Dim orange As Range
Set orange = ActiveDocument.Bookmarks("mytext").Range
If CheckBox2.Value = True Then
   orange.Font.Hidden = True
   ActiveWindow.View.ShowAll = False
Else
   orange.Font.Hidden = False
   ActiveWindow.View.ShowAll = True
End If
set oRange = Nothing
End Sub

Hope this helps.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top