Hide OptionButtons in a word document

B

Bshawn

I'm trying to use some option buttons in a Word document. When one is
clicked then the next section of the document is visible or hidden depending
on what the option button is choosing.

My problem is when the section to be hidden includes an option button, it
hides everything except the option button. I tried using the following code
for the first optionbutton.

ActiveDocument.Sections(2).Range.Font.Hidden = True

Is there a setting that is wrong, or additional code that I need to run?
Thanks.
 
B

Bshawn via OfficeKB.com

Does anyone else have this problem, but noone has a solution, or Does no one
else have the problem?

Thanks!
 
J

Jay Freedman

Well, it doesn't seem to be a common problem, but also it doesn't
appear to have a solution as long as you insist on using option
buttons that are ActiveX controls from the Control Toolbox.

The difficulty is that the ActiveX controls don't have a .Font
property and hence don't have a .Hidden property. They're OLEFormat
objects that do things their own way, independent of any font settings
in the surrounding text. I investigated, hoping to find a .Visible
property instead, but there is none.

If your document is a protected form using form fields, those will
obey the .Hidden value; instead of option buttons, you could use a
dropdown form field (even if it only has two items, such as Yes and
No).

Another alternative would be the sort of thing Greg Maxey shows at
http://gregmaxey.mvps.org/Add_Toggle_Objects.htm, but using characters
that look like option buttons instead of check boxes.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
B

Bshawn via OfficeKB.com

Thanks, this helps a lot. I have a follow up question. If I use the dropdown
form field, or a checkbox formfield, how can I reference those in VBA to
trigger a macro, or to have the macro look and see what option is chosen in
the drop down form field?

Thanks.

Jay said:
Well, it doesn't seem to be a common problem, but also it doesn't
appear to have a solution as long as you insist on using option
buttons that are ActiveX controls from the Control Toolbox.

The difficulty is that the ActiveX controls don't have a .Font
property and hence don't have a .Hidden property. They're OLEFormat
objects that do things their own way, independent of any font settings
in the surrounding text. I investigated, hoping to find a .Visible
property instead, but there is none.

If your document is a protected form using form fields, those will
obey the .Hidden value; instead of option buttons, you could use a
dropdown form field (even if it only has two items, such as Yes and
No).

Another alternative would be the sort of thing Greg Maxey shows at
http://gregmaxey.mvps.org/Add_Toggle_Objects.htm, but using characters
that look like option buttons instead of check boxes.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Does anyone else have this problem, but noone has a solution, or Does no one
else have the problem?
[quoted text clipped - 13 lines]
 
D

Doug Robbins - Word MVP

You run the macro by setting it to be run on exit from the formfield. That
is done via the Properties dialog for the formfield.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Bshawn via OfficeKB.com said:
Thanks, this helps a lot. I have a follow up question. If I use the
dropdown
form field, or a checkbox formfield, how can I reference those in VBA to
trigger a macro, or to have the macro look and see what option is chosen
in
the drop down form field?

Thanks.

Jay said:
Well, it doesn't seem to be a common problem, but also it doesn't
appear to have a solution as long as you insist on using option
buttons that are ActiveX controls from the Control Toolbox.

The difficulty is that the ActiveX controls don't have a .Font
property and hence don't have a .Hidden property. They're OLEFormat
objects that do things their own way, independent of any font settings
in the surrounding text. I investigated, hoping to find a .Visible
property instead, but there is none.

If your document is a protected form using form fields, those will
obey the .Hidden value; instead of option buttons, you could use a
dropdown form field (even if it only has two items, such as Yes and
No).

Another alternative would be the sort of thing Greg Maxey shows at
http://gregmaxey.mvps.org/Add_Toggle_Objects.htm, but using characters
that look like option buttons instead of check boxes.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Does anyone else have this problem, but noone has a solution, or Does no
one
else have the problem?
[quoted text clipped - 13 lines]
Is there a setting that is wrong, or additional code that I need to run?
Thanks.
 
J

Jay Freedman

And within the macro, you can act on what's selected in the dropdown
with code like this:

If ActiveDocument.FormFields("Dropdown1").Result = "Yes" Then
' do whatever is needed when 'Yes' is selected
MsgBox "it says Yes"
Else
' do whatever is needed when 'No' is selected
MsgBox "it says No"
End If

The 'name' of the formfield, shown above as "Dropdown1", is whatever
you put into the Bookmark box in the formfield's Properties dialog.
Note that the value to compare against the formfield's Result is
case-sensitive; that is, if the dropdown list contains "yes", that
won't match "Yes" but will match "yes".


You run the macro by setting it to be run on exit from the formfield. That
is done via the Properties dialog for the formfield.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Bshawn via OfficeKB.com said:
Thanks, this helps a lot. I have a follow up question. If I use the
dropdown
form field, or a checkbox formfield, how can I reference those in VBA to
trigger a macro, or to have the macro look and see what option is chosen
in
the drop down form field?

Thanks.

Jay said:
Well, it doesn't seem to be a common problem, but also it doesn't
appear to have a solution as long as you insist on using option
buttons that are ActiveX controls from the Control Toolbox.

The difficulty is that the ActiveX controls don't have a .Font
property and hence don't have a .Hidden property. They're OLEFormat
objects that do things their own way, independent of any font settings
in the surrounding text. I investigated, hoping to find a .Visible
property instead, but there is none.

If your document is a protected form using form fields, those will
obey the .Hidden value; instead of option buttons, you could use a
dropdown form field (even if it only has two items, such as Yes and
No).

Another alternative would be the sort of thing Greg Maxey shows at
http://gregmaxey.mvps.org/Add_Toggle_Objects.htm, but using characters
that look like option buttons instead of check boxes.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Does anyone else have this problem, but noone has a solution, or Does no
one
else have the problem?
[quoted text clipped - 13 lines]
Is there a setting that is wrong, or additional code that I need to run?
Thanks.
 
B

Bshawn via OfficeKB.com

Thanks, I think this will work for me. However, one thing I want to do is
hide some text when the Dropdown box is marked "no". I tried to do this
using

ActiveDocument.Sections(2).Range.Font.Hidden = True

However, it doesn't do anything. I think this is because to get the dropdown
to work, the form has to be protected, but then it won't let this code change
the font to hidden. Can I unprotect the form at the beginning of the macro,
and then protect it again at the end of the macro?

Thanks.

Jay said:
And within the macro, you can act on what's selected in the dropdown
with code like this:

If ActiveDocument.FormFields("Dropdown1").Result = "Yes" Then
' do whatever is needed when 'Yes' is selected
MsgBox "it says Yes"
Else
' do whatever is needed when 'No' is selected
MsgBox "it says No"
End If

The 'name' of the formfield, shown above as "Dropdown1", is whatever
you put into the Bookmark box in the formfield's Properties dialog.
Note that the value to compare against the formfield's Result is
case-sensitive; that is, if the dropdown list contains "yes", that
won't match "Yes" but will match "yes".
You run the macro by setting it to be run on exit from the formfield. That
is done via the Properties dialog for the formfield.
[quoted text clipped - 40 lines]
 
B

Bshawn via OfficeKB.com

I figured out my question. Here is my code:

ActiveDocument.Unprotect Password:=""
If ActiveDocument.FormFields("Yes").Result = "no" Then
ActiveDocument.Sections(2).Range.Font.Hidden = True
End If
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

This works fine when I run the macro by going to Tools, Macro, run. However,
when I try to use the dropdown box to start the macro, it doesn't do anything.


I chose the macro name in the Exit box in the drop down properties. Is there
something else I need to do? Thank You.

Thanks, I think this will work for me. However, one thing I want to do is
hide some text when the Dropdown box is marked "no". I tried to do this
using

ActiveDocument.Sections(2).Range.Font.Hidden = True

However, it doesn't do anything. I think this is because to get the dropdown
to work, the form has to be protected, but then it won't let this code change
the font to hidden. Can I unprotect the form at the beginning of the macro,
and then protect it again at the end of the macro?

Thanks.
And within the macro, you can act on what's selected in the dropdown
with code like this:
[quoted text clipped - 18 lines]
 
J

Jay Freedman

That's the correct code (although the Password argument isn't needed if
there is no password on the forms protection).

The thing about an Exit macro in a form field is that it executes when you
exit the field, not when you change the field contents. Does it work when
you tab out of the dropdown or click on another form field? (And maybe
obvious, but did you protect the form before trying to get it to run the
macro?)
I figured out my question. Here is my code:

ActiveDocument.Unprotect Password:=""
If ActiveDocument.FormFields("Yes").Result = "no" Then
ActiveDocument.Sections(2).Range.Font.Hidden = True
End If
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

This works fine when I run the macro by going to Tools, Macro, run.
However, when I try to use the dropdown box to start the macro, it
doesn't do anything.


I chose the macro name in the Exit box in the drop down properties.
Is there something else I need to do? Thank You.

Thanks, I think this will work for me. However, one thing I want to
do is hide some text when the Dropdown box is marked "no". I tried
to do this using

ActiveDocument.Sections(2).Range.Font.Hidden = True

However, it doesn't do anything. I think this is because to get the
dropdown to work, the form has to be protected, but then it won't
let this code change the font to hidden. Can I unprotect the form
at the beginning of the macro, and then protect it again at the end
of the macro?

Thanks.
And within the macro, you can act on what's selected in the dropdown
with code like this:
[quoted text clipped - 18 lines]
Is there a setting that is wrong, or additional code that I
need to run? Thanks.
 
B

Bshawn via OfficeKB.com

Thanks a lot. It's working great now. I am using it as an Entry macro, and
it runs as soon as I change it. I have one more question. If the dropdown
result is "Yes", then I want the hidden text to show again. Currently, if you
choose No, it hides it, but then you have to click on a different field and
then go back to the dropdown and choose yes to get it to do anything. Is
there a way to "Unselect" the drop down box from within the macro, so they
could choose "No", and then choose "Yes", and the text would change back,
without having to select another field in between? However, I don't want it
to be obvious that a different field is selected (for example, it cant scroll
down to the next field, if the next field is farther down in the document).

Thank you.

Jay said:
That's the correct code (although the Password argument isn't needed if
there is no password on the forms protection).

The thing about an Exit macro in a form field is that it executes when you
exit the field, not when you change the field contents. Does it work when
you tab out of the dropdown or click on another form field? (And maybe
obvious, but did you protect the form before trying to get it to run the
macro?)
I figured out my question. Here is my code:
[quoted text clipped - 30 lines]
 
J

Jay Freedman

The situation you're asking about is why we recommended making the macro an
Exit macro instead of an Entry macro. When it's an Entry macro, the macro
only runs at the time the cursor enters the dropdown. The user can change
the setting once, twice, or a thousand times but the macro won't do anything
at all until the cursor leaves and re-enters the field.

When you make it an Exit macro, the user can make as many changes as desired
as long as the cursor stays within the dropdown. As soon as the cursor exits
the dropdown, the macro runs and acts on whatever the final result is.

Your code does need a little addition to handle both "no" and "yes"
selections:

ActiveDocument.Unprotect Password:=""
If ActiveDocument.FormFields("Yes").Result = "no" Then
ActiveDocument.Sections(2).Range.Font.Hidden = True
Else
ActiveDocument.Sections(2).Range.Font.Hidden = False
End If
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

But recognize that *nothing* will be hidden or unhidden while the cursor is
still in the dropdown; that will happen only when the user tabs or clicks
out of the dropdown.
Thanks a lot. It's working great now. I am using it as an Entry
macro, and it runs as soon as I change it. I have one more question.
If the dropdown result is "Yes", then I want the hidden text to show
again. Currently, if you choose No, it hides it, but then you have to
click on a different field and then go back to the dropdown and
choose yes to get it to do anything. Is there a way to "Unselect"
the drop down box from within the macro, so they could choose "No",
and then choose "Yes", and the text would change back, without having
to select another field in between? However, I don't want it to be
obvious that a different field is selected (for example, it cant
scroll down to the next field, if the next field is farther down in
the document).

Thank you.

Jay said:
That's the correct code (although the Password argument isn't needed
if there is no password on the forms protection).

The thing about an Exit macro in a form field is that it executes
when you exit the field, not when you change the field contents.
Does it work when you tab out of the dropdown or click on another
form field? (And maybe obvious, but did you protect the form before
trying to get it to run the macro?)
I figured out my question. Here is my code:
[quoted text clipped - 30 lines]
Is there a setting that is wrong, or additional code that I
need to run? Thanks.
 

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