VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Error 91 - help plz

15 REPLIES 15
Reply
Message 1 of 16
Anonymous
289 Views, 15 Replies

Error 91 - help plz

Hi, I'd be very grateful for a pointer as to why this code doesn't work. It is to update the numeric value in a block attribute. I am very inexperienced in VBA so there is probably a glaring mistake. Thanks.

Sub updlev()

Dim varAttributes As Variant
Dim ssetObj As AcadSelectionSet
Dim i, j As Integer
Dim entSSet As AcadObject
Dim oldlev, newlev As Single
Dim newlevStr As String


On Error GoTo Errorhandler

ssetObj.Delete

Set ssetObj = ThisDrawing.SelectionSets.Add("BLOCKSET")

Dim gpCode(0) As Integer
Dim dataValue(0) As Variant
gpCode(0) = 0
dataValue(0) = "Insert"

Dim groupCode As Variant, dataCode As Variant
groupCode = gpCode
dataCode = dataValue

ssetObj.SelectOnScreen gpCode, dataValue


For i = 0 To ssetObj.Count - 1
Set entSSet = ssetObj.Item(i)

varAttributes = entSSet.GetAttributes

For j = LBound(varAttributes) To UBound(varAttributes)
If varAttributes(j).TagString = "0.00" Then
oldlev = Val(varAttributes(j).TextString)
newlev = oldlev - 28.16
newlevStr = Str(newlev)
varAttributes(j).TextString = newlevStr
Exit For
End If
Next j
Next i


ssetObj.Delete
Exit Sub 'avoid error handler

Errorhandler:
ssetObj.Delete
MsgBox "An error was encountered, please try again", vbOKOnly

End Sub
15 REPLIES 15
Message 2 of 16
Anonymous
in reply to: Anonymous

generally "this code doesn't work" isnt' a very specific diagnosis to go
on...
what line throws the error, what exactly is happening, etc are usually
helpful additions
:-)
in this case however it does appear to have an obvious problem, see below
hth
Mark

wrote in message news:5510350@discussion.autodesk.com...
Hi, I'd be very grateful for a pointer as to why this code doesn't work. It
is to update the numeric value in a block attribute. I am very inexperienced
in VBA so there is probably a glaring mistake. Thanks.

Sub updlev()

Dim varAttributes As Variant
Dim ssetObj As AcadSelectionSet
Dim i, j As Integer
Dim entSSet As AcadObject
Dim oldlev, newlev As Single
Dim newlevStr As String


On Error GoTo Errorhandler

----------- i would expect to get an error here, because ssetObj is Nothing
at this point
ssetObj.Delete

'----- other than that it looks like it would work

Set ssetObj = ThisDrawing.SelectionSets.Add("BLOCKSET")


Dim gpCode(0) As Integer
Dim dataValue(0) As Variant
gpCode(0) = 0
dataValue(0) = "Insert"

Dim groupCode As Variant, dataCode As Variant
groupCode = gpCode
dataCode = dataValue

ssetObj.SelectOnScreen gpCode, dataValue


For i = 0 To ssetObj.Count - 1
Set entSSet = ssetObj.Item(i)

varAttributes = entSSet.GetAttributes

For j = LBound(varAttributes) To UBound(varAttributes)
If varAttributes(j).TagString = "0.00" Then
oldlev = Val(varAttributes(j).TextString)
newlev = oldlev - 28.16
newlevStr = Str(newlev)
varAttributes(j).TextString = newlevStr
Exit For
End If
Next j
Next i


ssetObj.Delete
Exit Sub 'avoid error handler

Errorhandler:
ssetObj.Delete
MsgBox "An error was encountered, please try again", vbOKOnly

End Sub
Message 3 of 16
Anonymous
in reply to: Anonymous

Keith,

Here is how I would do it.

Joe ...

Sub updlev()
Dim varAttributes As Variant
Dim ssetObj As AcadSelectionSet
Dim i, j As Integer
Dim entSSet As AcadBlockReference 'changed the data type to blockref
Dim oldlev, newlev As Single
Dim newlevStr As String
'change the way you're handling errors
On Error Resume Next

ThisDrawing.SelectionSets("BLOCKSET").Delete
Set ssetObj = ThisDrawing.SelectionSets.Add("BLOCKSET")

On Error GoTo 0

Dim gpCode(0) As Integer
Dim dataValue(0) As Variant

gpCode(0) = 0
dataValue(0) = "Insert"

Dim groupCode As Variant, dataCode As Variant
groupCode = gpCode
dataCode = dataValue

ssetObj.SelectOnScreen gpCode, dataValue

For i = 0 To ssetObj.Count - 1
Set entSSet = ssetObj.Item(i)
If entSSet.HasAttributes Then 'added this line
varAttributes = entSSet.GetAttributes

For j = LBound(varAttributes) To UBound(varAttributes)
If varAttributes(j).TagString = "0.00" Then
oldlev = Val(varAttributes(j).TextString)
newlev = oldlev - 28.16
newlevStr = Str(newlev)
varAttributes(j).TextString = newlevStr
Exit For
End If
Next j
End If
Next i
End Sub


wrote in message news:5510350@discussion.autodesk.com...
Hi, I'd be very grateful for a pointer as to why this code doesn't work. It
is to update the numeric value in a block attribute. I am very inexperienced
in VBA so there is probably a glaring mistake. Thanks.

Sub updlev()

Dim varAttributes As Variant
Dim ssetObj As AcadSelectionSet
Dim i, j As Integer
Dim entSSet As AcadObject
Dim oldlev, newlev As Single
Dim newlevStr As String


On Error GoTo Errorhandler

ssetObj.Delete

Set ssetObj = ThisDrawing.SelectionSets.Add("BLOCKSET")

Dim gpCode(0) As Integer
Dim dataValue(0) As Variant
gpCode(0) = 0
dataValue(0) = "Insert"

Dim groupCode As Variant, dataCode As Variant
groupCode = gpCode
dataCode = dataValue

ssetObj.SelectOnScreen gpCode, dataValue


For i = 0 To ssetObj.Count - 1
Set entSSet = ssetObj.Item(i)

varAttributes = entSSet.GetAttributes

For j = LBound(varAttributes) To UBound(varAttributes)
If varAttributes(j).TagString = "0.00" Then
oldlev = Val(varAttributes(j).TextString)
newlev = oldlev - 28.16
newlevStr = Str(newlev)
varAttributes(j).TextString = newlevStr
Exit For
End If
Next j
Next i


ssetObj.Delete
Exit Sub 'avoid error handler

Errorhandler:
ssetObj.Delete
MsgBox "An error was encountered, please try again", vbOKOnly

End Sub
Message 4 of 16
Anonymous
in reply to: Anonymous

Sorry if my message wasn't clear enough - I thought the 'error 91' would be sufficient.
Strangely, when I got the error the debugger did not highlight any particular bit of code. I tried narrowing it down by commenting out sections but no luck.

Anyway thank you to both posters for quick fixes - very kind.

Keith
Message 5 of 16
Anonymous
in reply to: Anonymous

Can I ask one more newbie question.

If my macro is evaluating 48.36 - 25.16, how do I end up with 25.20 and NOT 25.2?

I've tried Format(x-y, "0.00") but it doesn't seem to do the trick.

thanks again.
Message 6 of 16
Anonymous
in reply to: Anonymous

Yes, I was a little heavy handed there, sorry...
the error 91 was a good enough indication in this case, and the error was
obvious enough
sometimes it's not that obvious and the more info given the better...

so you're saying when you get the error message, you hit the debug button
and it doesn't go to that line ???

Sub testSelSetDelete()
Dim ss As AcadSelectionSet
ss.Delete
End Sub

if you run that, and hit debug on the error dialog, does that not take you
to the second line?


wrote in message news:5510760@discussion.autodesk.com...
Sorry if my message wasn't clear enough - I thought the 'error 91' would be
sufficient.
Strangely, when I got the error the debugger did not highlight any
particular bit of code. I tried narrowing it down by commenting out sections
but no luck.

Anyway thank you to both posters for quick fixes - very kind.

Keith
Message 7 of 16
Anonymous
in reply to: Anonymous

wrote in message news:5510827@discussion.autodesk.com...
Can I ask one more newbie question.

If my macro is evaluating 48.36 - 25.16, how do I end up with 25.20 and NOT
25.2?

I've tried Format(x-y, "0.00") but it doesn't seem to do the trick.

thanks again.

it should work...try this
paste into immediate window
?Format(48.36 - 25.16, "0.00")
23.20

hth
Mark
Message 8 of 16
Anonymous
in reply to: Anonymous

The error was generated by the line you pointed out "Object or With block
variable not set".

It does not highlight the line in question like an "End Debug" does in VB
but it does the leave the cursor at the start of the line.

Joe ...


"MP" wrote in message
news:5510868@discussion.autodesk.com...
Yes, I was a little heavy handed there, sorry...
the error 91 was a good enough indication in this case, and the error was
obvious enough
sometimes it's not that obvious and the more info given the better...

so you're saying when you get the error message, you hit the debug button
and it doesn't go to that line ???

Sub testSelSetDelete()
Dim ss As AcadSelectionSet
ss.Delete
End Sub

if you run that, and hit debug on the error dialog, does that not take you
to the second line?


wrote in message news:5510760@discussion.autodesk.com...
Sorry if my message wasn't clear enough - I thought the 'error 91' would be
sufficient.
Strangely, when I got the error the debugger did not highlight any
particular bit of code. I tried narrowing it down by commenting out sections
but no luck.

Anyway thank you to both posters for quick fixes - very kind.

Keith
Message 9 of 16
Anonymous
in reply to: Anonymous

"Joe Sutphin" wrote in message
news:5510917@discussion.autodesk.com...
The error was generated by the line you pointed out "Object or With block
variable not set".

It does not highlight the line in question like an "End Debug" does in VB
but it does the leave the cursor at the start of the line.

Joe ...

that's interesting...vba ide does highlight errors here the same as vb ide.
are you actually saying that your acad vbaide does not highlight errors?

I wonder what the difference is?

Mark
Message 10 of 16
Anonymous
in reply to: Anonymous

I don't have a clue but I know what you're referring to and mine does not
highlight inside of VBA IDE on this particular error. The only option I get
is OK. Click on OK and poof ...

Joe ...

"MP" wrote in message
news:5510945@discussion.autodesk.com...
"Joe Sutphin" wrote in message
news:5510917@discussion.autodesk.com...
The error was generated by the line you pointed out "Object or With block
variable not set".

It does not highlight the line in question like an "End Debug" does in VB
but it does the leave the cursor at the start of the line.

Joe ...

that's interesting...vba ide does highlight errors here the same as vb ide.
are you actually saying that your acad vbaide does not highlight errors?

I wonder what the difference is?

Mark
Message 11 of 16
Anonymous
in reply to: Anonymous

"Joe Sutphin" wrote in message
news:5510975@discussion.autodesk.com...
I don't have a clue but I know what you're referring to and mine does not
highlight inside of VBA IDE on this particular error. The only option I get
is OK. Click on OK and poof ...

Joe ...

thats unusual!

what version?

are you saying you never get highlighting on an object not set condition?
but you do on other errors?
or only testing the op's code sample?
Mark
Message 12 of 16
Anonymous
in reply to: Anonymous

Never on an error of that type. Version 2007 but if my old memory serves me
I don't believe I've ever seen highlighting on an error of this type.

Joe ...

"MP" wrote in message
news:5511102@discussion.autodesk.com...
"Joe Sutphin" wrote in message
news:5510975@discussion.autodesk.com...
I don't have a clue but I know what you're referring to and mine does not
highlight inside of VBA IDE on this particular error. The only option I get
is OK. Click on OK and poof ...

Joe ...

thats unusual!

what version?

are you saying you never get highlighting on an object not set condition?
but you do on other errors?
or only testing the op's code sample?
Mark
Message 13 of 16
Anonymous
in reply to: Anonymous

amazing
I've never had an error that didn't highlight, afair

Mark

"Joe Sutphin" wrote in message
news:5511201@discussion.autodesk.com...
Never on an error of that type. Version 2007 but if my old memory serves me
I don't believe I've ever seen highlighting on an error of this type.

Joe ...
Message 14 of 16
Anonymous
in reply to: Anonymous

Hi -

You are right, the Format function does give the correct result. It is the conversion to a string which is omitting the trailing zero.

?str(43.30) gives 43.3

I've done a search in help and cannot find how to get the trailing zero included. Other than counting characters after the decimal point and adding a zero if necessary (which seems crazy), I cannot find a way of doing this.

Thanks Forget this post. Just realized Format is already returning a string, it works fine without the Str() function. Thanks.
Message was edited by: KeithXP
Message 15 of 16
Anonymous
in reply to: Anonymous

I don't do enough VB programming to be definitive, but I am using VBA as included with Acad 2007 (VBA v6.5). I do find that generally code errors are highlighted in yellow.

In the case of 'Run time error 91. Object Variable or With Block variable not set' , the error dialog has only OK or Cancel (no Debug button). On hitting OK I am simply returned to the editor with the cursor where it was when I ran the code.

Keith
Message 16 of 16
Anonymous
in reply to: Anonymous

I am not as experienced as Joe orMP, but I also remember a s few times where errors were not highlighted. I had to write to a text file throughout my code to pin down where the general error may be. I have also noticed a few things that I have done to improve the way my code works, and so I have not gotten this issue in a while. Perhaps Mark is using a better coding process. If so, it would be nice to see how he gets around it. I do not have the time to pick through this though.

Dave

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost