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

LVL 1
HakumAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
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

GrahamSkanRetiredCommented:
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

Roy CoxGroup Finance ManagerCommented:
My previous code should have been

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

Open in new window


However, if you stick to the same naming convention, i.e.

bm1  CheckBox1
bm2  CheckBox2

Try this code, it will allow you to add checkboxes and Bookmarks as required without changing the code.

Option Explicit


Private Sub CommandButton1_Click()

    Dim iX As Integer
    On Error Resume Next
    For iX = 1 To ActiveDocument.Bookmarks.Count
        ActiveDocument.Bookmarks("bm" & iX).Range.Font.Hidden = Me("CheckBox" & iX)
    Next iX
    On Error GoTo 0
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HakumAuthor Commented:
Thank you guys!!! you rock!!
Roy CoxGroup Finance ManagerCommented:
Glad we helped
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.