'Cannot be erased by caller' run-time error message - Trying to delete a block

'Cannot be erased by caller' run-time error message - Trying to delete a block

Anonymous
Not applicable
1,411 Views
3 Replies
Message 1 of 4

'Cannot be erased by caller' run-time error message - Trying to delete a block

Anonymous
Not applicable
In AutoCAD Map 2000 I am trying to check for an existing block and, if
exists, delete the block before inserting another block in Paper Space, ie.
the block "QSSwrLgnd" already exists and before I insert another block
"QSWtrLgnd", I want to delete the first block. However the program stops on
line 'ThisDrawing.Blocks.Item(i).Delete' and a run time error message
"Cannot be erased by caller" is displayed. What's up with this? I may be
way off base here with the code so be easy on me if I am (I don't do this
kind of thing oftenenough) - Thanks in advance for all help/suggestions/etc.

Dim EntGrp(0) As Integer
Dim EntPrp(0 TO 1) As Variant....

....Set ssnew2 = ThisDrawing.SelectionSets.Add("tmpblock")

EntGrp(0) = 2

EntPrp(0) = "QSSwrLgnd"
EntPrp(1) = "QSWtrLgnd"

ssnew2.Select acSelectionSetAll, EntGrp, EntPrp

If ssnew2.count >= 1 Then

For i = 0 To ssnew2.count - 1
ThisDrawing.Blocks.Item(i).Delete
Next i

End If

ThisDrawing.SelectionSets.Item("tmpblock").Delete....
0 Likes
1,412 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
Gregg,

Here is the problem & resolution from the API Help:
*******
When you delete an object in a collection, all remaining items in the
collection are reassigned a new index based on the current count. You should
therefore avoid loops that delete an object while iterating through the
collection. For example, the following VBA code will result in a runtime
error:

For i = 0 To ThisDrawing.Groups.Count - 1
ThisDrawing.Groups.Item(i).Delete
Next I

Instead, use the following VBA code to delete all members in a collection:

for each obj in ThisDrawing.Groups
obj.Delete
next obj

Scott



"Gregg Greer" wrote in message
news:1B08F56E6E9729C530CF45BA45131CF9@in.WebX.maYIadrTaRb...
> In AutoCAD Map 2000 I am trying to check for an existing block and, if
> exists, delete the block before inserting another block in Paper Space,
ie.
> the block "QSSwrLgnd" already exists and before I insert another block
> "QSWtrLgnd", I want to delete the first block. However the program stops
on
> line 'ThisDrawing.Blocks.Item(i).Delete' and a run time error message
> "Cannot be erased by caller" is displayed. What's up with this? I may be
> way off base here with the code so be easy on me if I am (I don't do this
> kind of thing oftenenough) - Thanks in advance for all
help/suggestions/etc.
>
> Dim EntGrp(0) As Integer
> Dim EntPrp(0 TO 1) As Variant....
>
> ....Set ssnew2 = ThisDrawing.SelectionSets.Add("tmpblock")
>
> EntGrp(0) = 2
>
> EntPrp(0) = "QSSwrLgnd"
> EntPrp(1) = "QSWtrLgnd"
>
> ssnew2.Select acSelectionSetAll, EntGrp, EntPrp
>
> If ssnew2.count >= 1 Then
>
> For i = 0 To ssnew2.count - 1
> ThisDrawing.Blocks.Item(i).Delete
> Next i
>
> End If
>
> ThisDrawing.SelectionSets.Item("tmpblock").Delete....
>
>
0 Likes
Message 3 of 4

Anonymous
Not applicable
Thanks for the info. Scott. I have revised my code as recommended and also
made some other changes but I now get the "run-time error '9': Subscript out
of range" message on line 'ssnew2.Select mode, Pt3, Pt4, EntGrp2, EntPrp2'.
I am completely stumped as to why this is happening as I have declared an
array with the correct number of elements for each point (Pt3 & Pt4) as well
as group code and name of blocks to filter. Again, all help/suggestions is
greatly appreciated.

Dim EntGrp(0) As Integer
Dim EntPrp(0 To 1) As Variant
Dim Pt3(0 To 2) As Double
Dim Pt4(0 To 2) As Double
Dim blockRefObj As AcadBlockReference
Dim ssnew2 As AcadSelectionSet....

'create a selection set in PaperSpace
....Set ssnew2 = ThisDrawing.SelectionSets.Add("tmpblock")

'Filter for Group code 2, the block name
EntGrp(0) = 2

'The name of the blocks to filter for
EntPrp(0) = "QSSwrLgnd"
EntPrp(1) = "QSWtrLgnd"

mode = acSelectionSetWindow
Pt3(0) = -0.25: Pt3(1) = -0.25: Pt3(2) = 0
Pt4(0) = 4: Pt4(1) = 12.5: Pt4(2) = 0

ssnew2.Clear

'find the block
ssnew2.Select mode, Pt3, Pt4, EntGrp, EntPrp

If ssnew2.count >= 1 Then
For Each blockRefObj In ssnew2
blockRefObj.Delete
Next
End If

ThisDrawing.SelectionSets.Item("tmpblock").Delete

insertionPnt(0) = -0.25: insertionPnt(1) = 1#: insertionPnt(2) = 0
Set blockRefObj = ThisDrawing.PaperSpace.InsertBlock(insertionPnt,
"QSSwrLgnd", 2#, 2#, 2#, 0)....


"Scott Friedrich" wrote in message
news:0490F54D902FB269764CB1B22A1CF25D@in.WebX.maYIadrTaRb...
> Gregg,
>
> Here is the problem & resolution from the API Help:
> *******
> When you delete an object in a collection, all remaining items in the
> collection are reassigned a new index based on the current count. You
should
> therefore avoid loops that delete an object while iterating through the
> collection. For example, the following VBA code will result in a runtime
> error:
>
> For i = 0 To ThisDrawing.Groups.Count - 1
> ThisDrawing.Groups.Item(i).Delete
> Next I
>
> Instead, use the following VBA code to delete all members in a collection:
>
> for each obj in ThisDrawing.Groups
> obj.Delete
> next obj
>
> Scott
>
>
>
> "Gregg Greer" wrote in message
> news:1B08F56E6E9729C530CF45BA45131CF9@in.WebX.maYIadrTaRb...
> > In AutoCAD Map 2000 I am trying to check for an existing block and, if
> > exists, delete the block before inserting another block in Paper Space,
> ie.
> > the block "QSSwrLgnd" already exists and before I insert another block
> > "QSWtrLgnd", I want to delete the first block. However the program stops
> on
> > line 'ThisDrawing.Blocks.Item(i).Delete' and a run time error message
> > "Cannot be erased by caller" is displayed. What's up with this? I may
be
> > way off base here with the code so be easy on me if I am (I don't do
this
> > kind of thing oftenenough) - Thanks in advance for all
> help/suggestions/etc.
> >
> > Dim EntGrp(0) As Integer
> > Dim EntPrp(0 TO 1) As Variant....
> >
> > ....Set ssnew2 = ThisDrawing.SelectionSets.Add("tmpblock")
> >
> > EntGrp(0) = 2
> >
> > EntPrp(0) = "QSSwrLgnd"
> > EntPrp(1) = "QSWtrLgnd"
> >
> > ssnew2.Select acSelectionSetAll, EntGrp, EntPrp
> >
> > If ssnew2.count >= 1 Then
> >
> > For i = 0 To ssnew2.count - 1
> > ThisDrawing.Blocks.Item(i).Delete
> > Next i
> >
> > End If
> >
> > ThisDrawing.SelectionSets.Item("tmpblock").Delete....
> >
> >
>
>
0 Likes
Message 4 of 4

Anonymous
Not applicable
You have a mistake defining the Group Code Value, it should be this:

Dim EntPrp(0) As Variant

EntPrp(0) = "QSSwrLgnd,QSWtrLgnd"

Scott


"Gregg Greer" wrote in message
news:C46E69F9109ECADA307414926908A90B@in.WebX.maYIadrTaRb...
> Thanks for the info. Scott. I have revised my code as recommended and
also
> made some other changes but I now get the "run-time error '9': Subscript
out
> of range" message on line 'ssnew2.Select mode, Pt3, Pt4, EntGrp2,
EntPrp2'.
> I am completely stumped as to why this is happening as I have declared an
> array with the correct number of elements for each point (Pt3 & Pt4) as
well
> as group code and name of blocks to filter. Again, all help/suggestions
is
> greatly appreciated.
>
> Dim EntGrp(0) As Integer
> Dim EntPrp(0 To 1) As Variant
> Dim Pt3(0 To 2) As Double
> Dim Pt4(0 To 2) As Double
> Dim blockRefObj As AcadBlockReference
> Dim ssnew2 As AcadSelectionSet....
>
> 'create a selection set in PaperSpace
> ....Set ssnew2 = ThisDrawing.SelectionSets.Add("tmpblock")
>
> 'Filter for Group code 2, the block name
> EntGrp(0) = 2
>
> 'The name of the blocks to filter for
> EntPrp(0) = "QSSwrLgnd"
> EntPrp(1) = "QSWtrLgnd"
>
> mode = acSelectionSetWindow
> Pt3(0) = -0.25: Pt3(1) = -0.25: Pt3(2) = 0
> Pt4(0) = 4: Pt4(1) = 12.5: Pt4(2) = 0
>
> ssnew2.Clear
>
> 'find the block
> ssnew2.Select mode, Pt3, Pt4, EntGrp, EntPrp
>
> If ssnew2.count >= 1 Then
> For Each blockRefObj In ssnew2
> blockRefObj.Delete
> Next
> End If
>
> ThisDrawing.SelectionSets.Item("tmpblock").Delete
>
> insertionPnt(0) = -0.25: insertionPnt(1) = 1#: insertionPnt(2) = 0
> Set blockRefObj = ThisDrawing.PaperSpace.InsertBlock(insertionPnt,
> "QSSwrLgnd", 2#, 2#, 2#, 0)....
>
>
> "Scott Friedrich" wrote in message
> news:0490F54D902FB269764CB1B22A1CF25D@in.WebX.maYIadrTaRb...
> > Gregg,
> >
> > Here is the problem & resolution from the API Help:
> > *******
> > When you delete an object in a collection, all remaining items in the
> > collection are reassigned a new index based on the current count. You
> should
> > therefore avoid loops that delete an object while iterating through the
> > collection. For example, the following VBA code will result in a runtime
> > error:
> >
> > For i = 0 To ThisDrawing.Groups.Count - 1
> > ThisDrawing.Groups.Item(i).Delete
> > Next I
> >
> > Instead, use the following VBA code to delete all members in a
collection:
> >
> > for each obj in ThisDrawing.Groups
> > obj.Delete
> > next obj
> >
> > Scott
> >
> >
> >
> > "Gregg Greer" wrote in message
> > news:1B08F56E6E9729C530CF45BA45131CF9@in.WebX.maYIadrTaRb...
> > > In AutoCAD Map 2000 I am trying to check for an existing block and, if
> > > exists, delete the block before inserting another block in Paper
Space,
> > ie.
> > > the block "QSSwrLgnd" already exists and before I insert another block
> > > "QSWtrLgnd", I want to delete the first block. However the program
stops
> > on
> > > line 'ThisDrawing.Blocks.Item(i).Delete' and a run time error message
> > > "Cannot be erased by caller" is displayed. What's up with this? I
may
> be
> > > way off base here with the code so be easy on me if I am (I don't do
> this
> > > kind of thing oftenenough) - Thanks in advance for all
> > help/suggestions/etc.
> > >
> > > Dim EntGrp(0) As Integer
> > > Dim EntPrp(0 TO 1) As Variant....
> > >
> > > ....Set ssnew2 = ThisDrawing.SelectionSets.Add("tmpblock")
> > >
> > > EntGrp(0) = 2
> > >
> > > EntPrp(0) = "QSSwrLgnd"
> > > EntPrp(1) = "QSWtrLgnd"
> > >
> > > ssnew2.Select acSelectionSetAll, EntGrp, EntPrp
> > >
> > > If ssnew2.count >= 1 Then
> > >
> > > For i = 0 To ssnew2.count - 1
> > > ThisDrawing.Blocks.Item(i).Delete
> > > Next i
> > >
> > > End If
> > >
> > > ThisDrawing.SelectionSets.Item("tmpblock").Delete....
> > >
> > >
> >
> >
>
>
0 Likes