Option Buttons

J

Johnnyboy5

Hi

I need a marco to add Yes No ? option buttons in a User Form. In
Word 2003

I have already added the option buttons into the user form (which has
other boxes in it) but I dont know what code to add and where it
should fit in with the other macro subs.

I also need to learning what to put the the word document after the
marco / user form has been made to see the results of the YES NO ?
answer.

Anyone got any good links - or macros to hand

Still learning....

Johnnyboy
 
G

Graham Mayor

You don't need to add the option buttons with a macro when you have already
added them to the userform manually.

You can read the values of the option buttons from your macro. For example -
imagine a userform with two option buttons with default names OptionButton1
and OptionButton2 and a command button with the default name CommandButton1
to process the form. Set the default state of one of the buttons to true.
The following code is associated with the command button and reads the
values of the two option buttons when that button is clicked.

As only one option button can be true at any one time only one of the
conditional checks will be true so the result is used to set a value in a
document variable, which can be displayed in the document with a docvariable
field. Equally it could write to a bookmark, or directly into the document,
but you may find docvariables easier to manage and program. The macro then
updates the fields in the document to reflect the new information and
unloads the form.


Option Explicit
Private oVars As Variables
Private Sub CommandButton1_Click()
Set oVars = ActiveDocument.Variables
If Me.OptionButton1.Value = True Then
oVars("varResult").Value = "yes"
End If
If Me.OptionButton2.Value = True Then
oVars("varResult").Value = "no"
End If
ActiveDocument.Fields.Update
Unload Me
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Johnnyboy5

You don't need to add the option buttons with a macro when you have already
added them to the userform manually.

You can read the values of the option buttons from your macro. For example -
imagine a userform with two option buttons with default names OptionButton1
and OptionButton2 and a command button with the default name CommandButton1
to process the form. Set the default state of one of the buttons to true.
The following code is associated with the command button and reads the
values of the two option buttons when that button is clicked.

As only one option button can be true at any one time only one of the
conditional checks will be true so the result is used to set a value in a
document variable, which can be displayed in the document with a docvariable
field. Equally it could write to a bookmark, or directly into the document,
but you may find docvariables easier to manage and program. The macro then
updates the fields in the document to reflect the new information and
unloads the form.

Option Explicit
Private oVars As Variables
Private Sub CommandButton1_Click()
Set oVars = ActiveDocument.Variables
If Me.OptionButton1.Value = True Then
    oVars("varResult").Value = "yes"
End If
If Me.OptionButton2.Value = True Then
    oVars("varResult").Value = "no"
End If
ActiveDocument.Fields.Update
Unload Me
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Thanks Graham, as helpful as ever.

John
 

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