Variable error

  • Thread starter Patrick Simonds
  • Start date
P

Patrick Simonds

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
 
J

Jonathan West

Patrick Simonds said:
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

The way you have declared it, Name is an object - referring to a document
variable. Before you can assign a value to it, you need to define which
document variable in which document the object refers to. This can be done
as follows

Set Name = ActiveDocument.Variables("Name")

By the way, I would recommend that you don't use names for your variables
such as Name, which are the same as keywords in VBA. Put a prefix on the
name, such as sName if the variable is a string, or oName if it an object of
some kind.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jean-Guy Marcil

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
 
C

Cindy M -WordMVP-

Hi Patrick,
Can anyone tell me why when I click on OptionButton1 I get the following
error:

object variable or with block variable not set
We need a bit more information, I think :) Where is this OptionButton? In
a userform, on a document, something else?

Which line of code is highlighted yellow when you get the error?

Actually, I don't think you can declare a variable as type "Variable",
although I would expect a different error message... Does anything change
if you use this, instead:
Dim oName as Variant
Here is my Subroutine:

Private Sub OptionButton1_Click()

Dim Name As Variable

If OptionButton1 = True Then
OptionButton5 = True
Name = "Patrick"
End If

End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
J

Jean-Guy Marcil

Cindy M -WordMVP- was telling us:
Cindy M -WordMVP- nous racontait que :
Hi Patrick,

We need a bit more information, I think :) Where is this
OptionButton? In a userform, on a document, something else?

Which line of code is highlighted yellow when you get the error?

Actually, I don't think you can declare a variable as type "Variable",

He meant a Doc Variable... ;-)

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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