Patrick Simonds was telling us:
Patrick Simonds nous racontait que :
Can anyone tell me why when I click on OptionButton1 I get the
following error:
object variable or with block variable not set
Here is my Subroutine:
Private Sub OptionButton1_Click()
Dim Name As Variable
If OptionButton1 = True Then
OptionButton5 = True
Name = "Patrick"
End If
End Sub
First, I think you get into the habit of not using default properties, so if
MSFT decides to change those, or not allow them in the future, your code
will still work, e.g.:
'_______________________________________
Private Sub OptionButton1_Click()
Dim Name As Variable
If OptionButton1.Value = True Then
OptionButton5.Value = True
Name = "Patrick"
End If
End Sub
'_______________________________________
Second, what is "Name"?
"Name" is a Word, VB and Office property; thus the compiler could get
confused and throw an error. In order to avoid confusion and/or errors, you
should not use predefined names as names/labels for your own Functions,
Subs, Constants or Variables.
Also,
Dim Name As Variable
defines a Word Document Variable, not a generic VB variable which should be
defined like:
Dim Name As String
Dim Name As Variant
Dim Name As Object
etc.
If you mean to use a Word document variable (which is what Word is trying to
do in your case, but you are not using the property correctly), try this:
'_______________________________________
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then
OptionButton5.Value = True
ActiveDocument.Variables("myName").Value = "Patrick"
End If
End Sub
'_______________________________________
But if you mean a VB variable, try:
'_______________________________________
Private Sub OptionButton1_Click()
Dim myName As String
If OptionButton1.Value = True Then
OptionButton5.Value = True
myName = "Patrick"
End If
End Sub
'_______________________________________
Finally, remember that unless you have code that explicitely chganges option
button behaviour, or set up your Userform accordingly (with frames for
example), option buttons are mutually exclusive, so, with your code, when
the user clicks on OptionButton1, OptionButton5 will be set to True, and in
so doing, OptionButton1 will automatically become False.
If this is not what you want, instead of changing option button behaviour
(which can be confusing to users) use Checkboxes which are designed to be
non exclusive.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org