Can I Toggle Display of Form Controls On and Off?

M

Marcia

I need to create a template that will toggle the display of a
drop-down box on and off. I want the drop-down box to become visible
if the user selects a checkbox and, likewise, I want the drop-down box
to remain hidden if the user does NOT select the checkbox.

I looked through the properties for a "Visible" code, but did not see
anything that would help me. I am a very new beginner, however, and
may have overlooked it.

Any advice would be greatly appreciated. I am using Word 2000.

Thanks!

Jessi
 
D

Doug Robbins - Word MVP

Hi Marcia,

The following would work as long as the display of hidden text is turned off
under Tools>Options>View

If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.Unprotect
ActiveDocument.FormFields("DropDown1").Range.Font.Hidden = False
ActiveDocument.Protect wdAllowOnlyFormFields, NoREset
End If

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
M

Marcia

Thanks so much! This worked very well. I also added an "Else" line
to disable and hide the control when the checkbox was NOT selected.

I have many, many checkboxes in this one file, however, and I was
trying to think of a way to pass this one macro to every checkbox
(instead of having a separate macro for each checkbox), but I can't
think of a way. Is there a way to pass the name of the checkbox to a
variable (which would then be used in the macro)?

Thanks for your help!

Jessi
 
D

Doug Robbins - Word MVP

Hi Marcia,

You should be able to make use of the information in the article "How to
find the name of the current formfield" at:

http://www.mvps.org/word/FAQs/TblsFldsFms/GetCurFmFldName.htm
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.


Hope this helps
Doug Robbins - Word MVP
 
M

Marcia

Thanks for your reply! I have played with this for several days and
can't figure it out.

Here is my original code, based on checkbox and dropdown controls
which were named "Check1" and "DropDown1" respectively. (It works, but
I would have to recreate this macro many times, based on different
control names):

Sub ToggleFormField ()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.Unprotect
With ActiveDocument.FormFields("Check1")
.Range.Font.Hidden = False
.Enabled = True
End With
ActiveDocument.Protect wdAllowOnlyFormFields, NoREset
Else
ActiveDocument.Unprotect
With ActiveDocument.FormFields("Check1")
.Range.Font.Hidden = True
.Enabled = False
End With
ActiveDocument.Protect wdAllowOnlyFormFields, NoREset
End If
End Sub


If, however, I add the following code in front of it, I can't get it
to work. (I assumed that it was using a generic "Name," so I renamed
my checkbox and dropdown controls to "Name," but I guess that I'm
totally missing the concept since it doesn't work.)

If Selection.FormFields.Count = 1 Then
'No textbox but a check- or listbox
MsgBox Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count >
0 Then
MsgBox Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If

What am I missing? Sorry to be so slow.

Jessi
 
D

Doug Robbins - Word MVP

Hi Marcia,

If you checkboxes and dropdowns have bookmark names Check1, Check2, Check3
etc. and DropDown1, DropDown2, DropDown3 etc., then you can use:

Dim i As Integer, CB As String, DD As String
i = Mid(Selection.FormFields(1).Name, 6)
CB = "Check" & i
DD = "DropDown" & i
ActiveDocument.Unprotect
If ActiveDocument.FormFields(CB).CheckBox.Value = True Then
ActiveDocument.FormFields(DD).Range.Font.Hidden = False
Else
ActiveDocument.FormFields(DD).Range.Font.Hidden = True
End If
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset

in a macro that is run on exit from each checkbox.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
M

Marcia

Hi, Doug. Thanks so much for your help. I added the code, but when
I test it, I get the following error message:

Run-time Error 13 -- Type Mismatch

Do I need to change the datatype somewhere? Here is what I have so
far:

Sub ToggleFormField()
'
Dim i As Integer, CB As String, DD As String
i = Mid(Selection.FormFields(1).Name, 6)
CB = "Check" & i
DD = "DropDown" & i
ActiveDocument.Unprotect
If ActiveDocument.FormFields(CB).CheckBox.Value = True Then
With ActiveDocument.FormFields(DD)
.Range.Font.Hidden = False
.Enabled = True
End With
Else
With ActiveDocument.FormFields(DD)
.Range.Font.Hidden = True
.Enabled = False
End With
End If
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset

End Sub

Thanks!
 
D

Doug Robbins - Word MVP

Hi Marcia,

Are your formfields named the way that I mentioned?

What line of code is highlighted when you get the error message?

It worked for me.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
M

Marcia

Hi, Doug

Yes, the form fields are named "Check1, Check2, etc." and the
dropdown fields are named "DropDown1, DropDown2, etc."

It seems to work just fine until I try to tab through a
previously-selected checkbox WITHOUT deselecting it... then, as I
continue tabbing through the dropdown field, it hangs up (as it exits
the dropdown field) and displays the error message. But as long as I
deselect the checkbox first, it does fine.

Marcia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top