Link to home
Start Free TrialLog in
Avatar of Harsh Kumar
Harsh KumarFlag for Denmark

asked on

VBA - Word, hide/unhide text with checkbox in userform

Hi guys,

i'm trying to hide/unhide text in the document from a userform with a checkbox...

for some reason its not working for me... this is the code i'm working with

Private Sub CommandButton1_Click()

If UserForm1.CheckBox1 = True Then
UnhideText (bm1.Range)
End If

If UserForm1.CheckBox1 = False Then
HideText (bm1.Range)
End If

If UserForm1.CheckBox2 = True Then
UnhideText (bm2.Range)
End If

If UserForm1.CheckBox2 = False Then
HideText (bm2.Range)
End If

End Sub


Public Sub HideText(ByVal rng As Word.Range)
    rng.Font.Hidden = True
End Sub

Public Sub UnhideText(ByVal rng As Word.Range)
    rng.Font.Hidden = False
End Sub

Open in new window

Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

I don't know much about Word VBA, but are bm1, etc BookMarks? If so this works for me

Private Sub CommandButton1_Click()
ActiveDocument.Bookmarks("bm1").Range.Font.Hidden = True = UserForm1.CheckBox1
'etc
End Sub 

Open in new window

If you already have bm1 instantiated as a bookmark object, then your code should work, but I suspect that the bookmark name is bm1. Roy shows that with some very elegant code, but this is how you could adapt your own code:
Private Sub CommandButton1_Click()
Dim bm1 As Bookmark
Set bm1 = ActiveDocument.Bookmarks("bm1")

If UserForm1.CheckBox1 = True Then
UnhideText bm1.Range
End If

If UserForm1.CheckBox1 = False Then
HideText bm1.Range
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Harsh Kumar

ASKER

Thank you guys!!! you rock!!
Glad we helped