Consulting

Results 1 to 4 of 4

Thread: Toggle visibility of a table based on a checkbox value

  1. #1
    VBAX Newbie
    Joined
    Mar 2019
    Posts
    5
    Location

    Toggle visibility of a table based on a checkbox value

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Mar 2019
    Posts
    5
    Location
    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!!

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •