Avatar of dbote
Flag for United States of America asked on

VBA Controls Class

A UserForm for my MS Word  document has a button click that loops through the Controls on the form.

Private Sub cmdBack_Click()
  Dim ctrl As Control
  For Each ctrl In frmTest.Controls    
        If ctrl.Name = "fra1" Then ctrl.Visible = False
End Sub

Stepping through everything seems to work great BUT the control named fra1 never goes invisible.

In the Immediate window the control referenced with ctrl.visible returns False while the control referenced with fra1.visible returns True

If I change the code to fra1.visible = False the control does become invisible.

Why are these two code segments getting different results?

Thank you,

Microsoft Word

Avatar of undefined
Last Comment

8/22/2022 - Mon
Chris Bottomley

Is it finding that control on the form frmtest? ... try changing ctrl.visible = false to msgbox "here"


In the For Each loop the control is definitely being found.  During debug I have cycled through stopping with each control and the control named fra1 definitely exists and is being found.
Chris Bottomley

fra1 would work irrespective of caps whereas the name check would fail depending on vcase of the name ... so is the name definitely lower case?

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

I'm hoping that other will learn from my "mistake".