Galahadoc
11-10-2008, 05:58 PM
Hi guys,
I found this (had hyperlink to the code but apparently with under 5 posts that is a no-no so i'll post it at the bottom of this post) code by Fumei which uses a checkbox (or other objects) to hide and un-hide tables (or text, or, from what i can gather, anything you want). After looking around for ages for something that will do what i wanted, finally thinking word just didn't have the functionality to do what was needed, i stumbled across this site.
Anyway, the problem i have run into is the way the checkboxes are working. I have 4 tables with a checkbox following the title which sits above each of the tables. So by default i would like the boxes to be unchecked and the table invisible, then when you check the box then the table appears, i've worked out this much through trial and error. But for some reason using the 2nd, 3rd, and 4th boxes effect all 4 tables in 1 way or another. I believe it's mainly effecting them when you check the box to open the table, it opens all of the tables, they then all collapse when you un-check that box.
Keeping in mind this is the first time i have actually knowingly used VBA, i somewhat understand the absolute basics only through knowing the absolute basics of other coding languages.
I was going to simply PM Fumei but given the knowledge of most people here i was hoping that maybe someone else could help aswell.
For the purpose of this document i use Word 2003; i have Word 2007 at my other house, is this going to effect the way this document works? (more curiosity then anything else).. I attached the document for reference and the code i have used is as follows:
Code from my document:
Option Explicit
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 = False Then
With Selection.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Selection.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = False
End With
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
Private Sub CheckBox2_Change()
Call ShowHideTable2
End Sub
Sub ShowHideTable2()
With Selection
.GoTo What:=wdGoToTable, Which:=wdGoToNext, _
Count:=1, Name:=""
.Tables(1).Select
End With
If CheckBox2.Value = False Then
With Selection.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Selection.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = False
End With
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
Sub CheckBox3_Click()
Call ShowHideTable3
End Sub
Sub ShowHideTable3()
With Selection
.GoTo What:=wdGoToTable, Which:=wdGoToNext, _
Count:=1, Name:=""
.Tables(1).Select
End With
If CheckBox3.Value = False Then
With Selection.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Selection.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = False
End With
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
Sub CheckBox4_Click()
Call ShowHideTable4
End Sub
Sub ShowHideTable4()
With Selection
.GoTo What:=wdGoToTable, Which:=wdGoToNext, _
Count:=1, Name:=""
.Tables(1).Select
End With
If CheckBox4.Value = False Then
With Selection.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Selection.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = False
End With
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
Code from Fumei:
Option Explicit
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
With Selection.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Selection.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = True
End With
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
' the other View properties
' if you want to still see paragraph marks, you
' must explicitly turn it on = True
' .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
' this is to do the same thing with a bookmark ("mytext")
' using a second checkbox (Checkbox2)
Sub CheckBox2_Change()
Call ShowHideBookmark
End Sub
Sub ShowHideBookmark()
Dim orange As Range
Set orange = ActiveDocument.Bookmarks("mytext").Range
If CheckBox2.Value = True Then
With orange.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With orange.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = True
End With
End If
End Sub
Thanks guys, hope you can help.
I found this (had hyperlink to the code but apparently with under 5 posts that is a no-no so i'll post it at the bottom of this post) code by Fumei which uses a checkbox (or other objects) to hide and un-hide tables (or text, or, from what i can gather, anything you want). After looking around for ages for something that will do what i wanted, finally thinking word just didn't have the functionality to do what was needed, i stumbled across this site.
Anyway, the problem i have run into is the way the checkboxes are working. I have 4 tables with a checkbox following the title which sits above each of the tables. So by default i would like the boxes to be unchecked and the table invisible, then when you check the box then the table appears, i've worked out this much through trial and error. But for some reason using the 2nd, 3rd, and 4th boxes effect all 4 tables in 1 way or another. I believe it's mainly effecting them when you check the box to open the table, it opens all of the tables, they then all collapse when you un-check that box.
Keeping in mind this is the first time i have actually knowingly used VBA, i somewhat understand the absolute basics only through knowing the absolute basics of other coding languages.
I was going to simply PM Fumei but given the knowledge of most people here i was hoping that maybe someone else could help aswell.
For the purpose of this document i use Word 2003; i have Word 2007 at my other house, is this going to effect the way this document works? (more curiosity then anything else).. I attached the document for reference and the code i have used is as follows:
Code from my document:
Option Explicit
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 = False Then
With Selection.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Selection.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = False
End With
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
Private Sub CheckBox2_Change()
Call ShowHideTable2
End Sub
Sub ShowHideTable2()
With Selection
.GoTo What:=wdGoToTable, Which:=wdGoToNext, _
Count:=1, Name:=""
.Tables(1).Select
End With
If CheckBox2.Value = False Then
With Selection.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Selection.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = False
End With
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
Sub CheckBox3_Click()
Call ShowHideTable3
End Sub
Sub ShowHideTable3()
With Selection
.GoTo What:=wdGoToTable, Which:=wdGoToNext, _
Count:=1, Name:=""
.Tables(1).Select
End With
If CheckBox3.Value = False Then
With Selection.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Selection.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = False
End With
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
Sub CheckBox4_Click()
Call ShowHideTable4
End Sub
Sub ShowHideTable4()
With Selection
.GoTo What:=wdGoToTable, Which:=wdGoToNext, _
Count:=1, Name:=""
.Tables(1).Select
End With
If CheckBox4.Value = False Then
With Selection.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Selection.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = False
End With
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
Code from Fumei:
Option Explicit
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
With Selection.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Selection.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = True
End With
With Selection
.Collapse direction:=wdCollapseStart
.MoveLeft unit:=wdCharacter, Count:=1
End With
End If
End Sub
' the other View properties
' if you want to still see paragraph marks, you
' must explicitly turn it on = True
' .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
' this is to do the same thing with a bookmark ("mytext")
' using a second checkbox (Checkbox2)
Sub CheckBox2_Change()
Call ShowHideBookmark
End Sub
Sub ShowHideBookmark()
Dim orange As Range
Set orange = ActiveDocument.Bookmarks("mytext").Range
If CheckBox2.Value = True Then
With orange.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With orange.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = True
End With
End If
End Sub
Thanks guys, hope you can help.