Windows Vista - Hide Text using VBA and either Styles or Bookmarks

Asked By N on 12-Sep-11 01:59 PM
I am working with a form document in Word 2007.  I need to use a menu system where once a menu option has been checked using a legacy form field, the sub options for that menu will be displayed.  If the menu option is not checked, the sub options will be hidden. 

I would like to use either styles or bookmarks to do this, but am open to any suggestions. 

I have used the code below and sometimes it works, other times it won't.  Any suggestions?

My current code:


Sub Menu1()

    Dim Menu1 As FormField
    Set Menu1 = ActiveDocument.FormFields("Menu1Ckbx")
   
    Dim bProtected As Boolean
    Dim sPassword As String
   
    sPassword = ""
   
    'Check for Protection
    If ActiveDocument.ProtectionType <> wdNoProtection Then
      bProtected = True
      ActiveDocument.Unprotect Password:=sPassword
    End If
   
   
    'Hide Text if Checkbox not selected
     
    If Menu1.CheckBox.Value = True Then
     
      ActiveDocument.Bookmarks("Menu1Text").Range.Font.Hidden = False
      ActiveDocument.Bookmarks("Menu1Opt").Range.Font.Hidden = False
   
    Else
    
      ActiveDocument.Bookmarks("Menu1Text").Range.Font.Hidden = False
      ActiveDocument.Bookmarks("Menu1Opt").Range.Font.Hidden = True

    End If

    'Reprotect the document.
    If bProtected = True Then
      ActiveDocument.Protect _
      Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=sPassword
    End If


End Sub

smr replied to N on 12-Sep-11 02:06 PM
hi

refer this example

Sub Peekaboo(oSh As Shape)
' Hides/makes visible the shape's text
 
  With oSh.TextFrame.TextRange
    ' If it has text ...
    If Len(.Text) > 0 Then
      ' Store the text in a tag so we can retrieve it later
      oSh.Tags.Add "PeekabooText", .Text
      ' Now blank the text
      .Text = ""
    Else
      .Text = oSh.Tags("PeekabooText")
    End If
  End With
 
  ' Update: added this line to make it work in PPT 2007
  ' Leaving it in should not affect earlier versions of PPT
  SlideShowWindows(1).View.GotoSlide (oSh.Parent.SlideIndex)
 
End Sub
 
Sub ResetPeekaboos()
' Resets the shapes with peekaboo text to make the text invisible
 
  Dim oSh As Shape
  Dim oSl As Slide
 
  For Each oSl In ActivePresentation.Slides
    For Each oSh In oSl.Shapes
      If Len(oSh.Tags("PeekabooText")) > 0 Then
        oSh.TextFrame.TextRange.Text = ""
      End If
    Next    ' oSh
  Next    ' oSl
 
End Sub
 
Sub UnhidePeekaboos()
' Resets the shapes with peekaboo text to make the text visible
 
  Dim oSh As Shape
  Dim oSl As Slide
 
  For Each oSl In ActivePresentation.Slides
    For Each oSh In oSl.Shapes
      If Len(oSh.Tags("PeekabooText")) > 0 Then
        oSh.TextFrame.TextRange.Text = oSh.Tags("PeekabooText")
      End If
    Next    ' oSh
  Next    ' oSl
 
End Sub
 
Sub HidePeekaboos()
' Resets the shapes with peekaboo text to make the text invisible
' You must have activated the text as peekaboo one time for this to work
 
  Dim oSh As Shape
  Dim oSl As Slide
 
  For Each oSl In ActivePresentation.Slides
    For Each oSh In oSl.Shapes
      If Len(oSh.Tags("PeekabooText")) > 0 Then
        oSh.TextFrame.TextRange.Text = ""
      End If
    Next    ' oSh
  Next    ' oSl
 
End Sub

refer
http://www.sikhphilosophy.net/information-technology/6583-vba-hide-show-label-text-box.html
http://www.pptfaq.com/FAQ00662.htm
Devil Scorpio replied to N on 12-Sep-11 02:39 PM
Hi,

Have you used the right option button name in your Range.Hidden line of code?
ActiveDocument.Bookmarks("YesOptionbuttonName").Range.Font.Hidden = True

And is the option button inside any other control like a Paragraph?
Then try hiding that control first.

Also , If you are running this inside the macro, the macro could be getting disabled.