PDA

View Full Version : [SOLVED:] Toggle visibility of a table based on a checkbox value



CraigP
03-23-2019, 06:16 AM
Hi, I'm trying to hide or show tables based on the status of checkboxes.
I'm using code that "Fumei" provided in these forums back in 2002.
while it does work no matter which checkbox I check all of the targeted tables change rather than just the one the code instructs....I cannot fathom why. Please help.
i have bookmarked each table 1...16. I'm trying to hide or show three of the tables individually based on different activeX checkboxes - but no matter how I try to contain the code to only effect one table, all three change together! Thank you for your support.

Sub CheckBox2_Change() Call ShowHideBookmark End Sub Sub ShowHideBookmark() Dim orange As Range Set orange = ActiveDocument.Bookmarks("Tab1").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

gmaxey
03-23-2019, 08:54 AM
Do you have your bookmarks defined properly? This seems to work:


Option Explicit
Sub CheckBox1_Change()
ShowHideBookmark 1
End Sub
Sub CheckBox2_Change()
ShowHideBookmark 2
End Sub
Sub CheckBox3_Change()
ShowHideBookmark 3
End Sub
Sub ShowHideBookmark(lngTable As Long)
Dim oILS As InlineShape
Dim oAX
Dim oRng As Range
For Each oILS In ActiveDocument.InlineShapes
If oILS.OLEFormat.Object.Name = "CheckBox" & lngTable Then
Set oAX = oILS.OLEFormat.Object
Exit For
End If
Next oILS
If Not oAX Is Nothing Then
Set oRng = ActiveDocument.Bookmarks("Tab" & lngTable).Range
If oAX.Value = True Then
oRng.Font.Hidden = True
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
oRng.Font.Hidden = False
End If
End If
lbl_Exit:
Exit Sub
End Sub

CraigP
03-24-2019, 01:03 AM
Thank you soo much Greg...work really well....mate I have been working this for a month....I should have reached out earlier. Thanks again!!

gmaxey
03-24-2019, 07:47 AM
Craig,

Glad to help. You should probably change the main routine as follows as should there ever be other types of InlineShapes (that's what ActiveX controls are) in the document, it would error:


Sub ShowHideBookmark(lngTable As Long)
Dim oILS As InlineShape
Dim oAX
Dim oRng As Range
For Each oILS In ActiveDocument.InlineShapes
If oILS.OLEFormat.ClassType = "Forms.CheckBox.1" Then
If oILS.OLEFormat.Object.Name = "CheckBox" & lngTable Then
Set oAX = oILS.OLEFormat.Object
Exit For
End If
End If
Next oILS
If Not oAX Is Nothing Then
Set oRng = ActiveDocument.Bookmarks("Tab" & lngTable).Range
If oAX.Value = True Then
oRng.Font.Hidden = True
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
oRng.Font.Hidden = False
End If
End If
lbl_Exit:
Exit Sub
End Sub