Runtime Error -2147467259

Runtime Error -2147467259

Anonymous
Not applicable
2,042 Views
20 Replies
Message 1 of 21

Runtime Error -2147467259

Anonymous
Not applicable
Hi,
I have attached several XREFs in a drawing. After plotting
I need to detach the XREFs. So far I use the following code:

Dim blockObjekt As AcadBlock

For Each blockObjekt In ThisDrawing.Blocks

if blockObjekt.IsXRef Then
If blockObjekt.name <> "" Then
Debug.Print blockObjekt.name
ThisDrawing.Blocks.Item(blockObjekt.name).Detach
End If
End If

Next blockObjekt

The first XREF is detached, after that I get a Runtime Error -2147467259 and the routine ends.

Does anybody know what happens?

Carmen
0 Likes
2,043 Views
20 Replies
Replies (20)
Message 2 of 21

Anonymous
Not applicable
I got your code to run past the first block by replacing "Next blockObjekt"
with just "Next". Since you had deleted blockObjekt, when it tried to do
the Next statement, the variable blockObjekt was no longer valid.

But now your code still crashed after the LAST block. My guess is that
somehow it was trying to go "Next" PAST the last item in the collection.
You could use an "On Error" handler to ignore this specific error number,
and move on, but it's not a very pretty solution.


Hope this helps,

James
0 Likes
Message 3 of 21

Anonymous
Not applicable
As an aside, you can simplify your Detach statement to

blockObject.Detach

Sorry, though... this doesn't change the way the program runs.

James
0 Likes
Message 4 of 21

Anonymous
Not applicable
OK... I made the change to use Error handling. I'm a little embarassed to
show this because I'm sure it abuses the true spirit of error handling...
but it runs.

James

Dim blockObjekt As AcadBlock
On Error Resume Next
For Each blockObjekt In ThisDrawing.Blocks
if blockObjekt.IsXRef Then
If blockObjekt.name <> "" Then
Debug.Print blockObjekt.name
blockObjekt.Detach
End If
End If
Next
If Err.Number <> -2147467259 then
Dim ErrNum As Long
ErrNum = Err.Number ' so other errors will still stop program...
On Error GoTo 0
Err.Raise (ErrNum)
End If
0 Likes
Message 5 of 21

Anonymous
Not applicable
Hi,

Well, now you have jinxed my code as well. I had exactly the same code as
you, except for the blockObject name and when I tested it a couple of weeks
ago it worked fine, now when I test it, I get the same error.

...... further testing .......

OK, it seems I only get the problem when I try and detach an xrefed drawing
in that contains blocks. I would guess that the code is probably going
through all block definitions, including those in a an xref. No doubt at
some point the xref becomes detached and the block reference no longer
exists and you have the problem and your code panics.

I would be interested to hear if there is a better solution than trapping
the error as suggested by James.

Regards

Sean Bartleet



"xiexieanke" wrote in message
news:[email protected]...
> Hi,
> I have attached several XREFs in a drawing. After plotting
> I need to detach the XREFs. So far I use the following code:
> Dim blockObjekt As AcadBlock
>
> For Each blockObjekt In ThisDrawing.Blocks
>
> if blockObjekt.IsXRef Then
> If blockObjekt.name <> "" Then
> Debug.Print blockObjekt.name
> ThisDrawing.Blocks.Item(blockObjekt.name).Detach
> End If
> End If
>
> Next blockObjekt
>
> The first XREF is detached, after that I get a Runtime Error -2147467259
and the routine ends.
>
> Does anybody know what happens?
>
> Carmen
>
0 Likes
Message 6 of 21

Anonymous
Not applicable
I profess to know next-to-nothing about AutoCAD, however I just did a bit of investigation and on a new empty drawing I appear to have three Blocks predefined:

   *Model_Space

   *Paper_Space

   *Paper_Space0

I suspect you can't get rid of these. If that is true, a couple of solutions spring to mind (other than the Resume Next). You could do an If-test inside the loop to make sure the Block.Name is not one of these special names. Or you could skip these special ones by changing the loop to:


For J = 3 to ThisDrawing.Blocks.Count - 1
  Set blockObjekt = ThisDrawing.Blocks(J)
  etc

However I don't know enough to know whether the number of predefined blocks is constant, or the names of them are constant. Any knowledgable ones care to comment?



Regards



Wayne Ivory
0 Likes
Message 7 of 21

Anonymous
Not applicable
Hi,

If your drawing has multiple xref's attached all with blocks defined in them
your code leaves some Xref's attached. Hopefully there is a way out there to
do this.

Regards.

Sean Bartleet

"James Belshan" wrote in message
news:[email protected]...
> OK... I made the change to use Error handling. I'm a little embarassed to
> show this because I'm sure it abuses the true spirit of error handling...
> but it runs.
>
> James
>
> Dim blockObjekt As AcadBlock
> On Error Resume Next
> For Each blockObjekt In ThisDrawing.Blocks
> if blockObjekt.IsXRef Then
> If blockObjekt.name <> "" Then
> Debug.Print blockObjekt.name
> blockObjekt.Detach
> End If
> End If
> Next
> If Err.Number <> -2147467259 then
> Dim ErrNum As Long
> ErrNum = Err.Number ' so other errors will still stop program...
> On Error GoTo 0
> Err.Raise (ErrNum)
> End If
>
>
0 Likes
Message 8 of 21

Anonymous
Not applicable
Hi,

.............Further hacking..............

I got the code below to work, It seems that blocks contained inside an xref
are also listed in the blocks collection, when you remove the xref, the for
eachblock in this drawing poops in its pants when suddenly a block that
should be there is not there anymore (because the xref has been detached).
It guess that it is not a good idea to do brain surgery on yourself.

The next trick is to recognize nested xrefs and make sure that you don't try
detach an xref that has already been removed because it was really a nested
xref and the parent was removed. Does anyone have any ideas of how I can
test for a nested xref and thereby avoid adding it to my list of names?

Sub TTest()
Dim vList() As Variant
Dim I As Integer
Dim blkMyBlock As AcadBlock

I = -1
Debug.Print vbCrLf & "XXXXXXXXXXXXXXXXX"
For Each blkMyBlock In ThisDrawing.Blocks
Debug.Print blkMyBlock.Name, blkMyBlock.IsXRef, blkMyBlock.OwnerID
If blkMyBlock.IsXRef Then
If I < 0 Then I = 0
ReDim Preserve vList(0 To I)
vList(I) = blkMyBlock.Name
I = I + 1
End If
Next blkMyBlock
If I > 0 Then
' For I = 0 To UBound(vList)
' ThisDrawing.Blocks.Item(vList(I)).Detach
' Next I
End If
End Sub

Please let me know if there is a better way and if you ahve any ide of how
to test for a nested xref.

Regards

Sean Bartleet
0 Likes
Message 9 of 21

Anonymous
Not applicable
Hi,

Me again.

If you add a "On Error Resume Next" statement if the item no longer exists
it will just be skipped and the code will carry on regardless.


Sub TTest()
Dim vList() As Variant
Dim I As Integer
Dim blkMyBlock As AcadBlock

I = -1
Debug.Print vbCrLf & "XXXXXXXXXXXXXXXXX"
For Each blkMyBlock In ThisDrawing.Blocks
Debug.Print blkMyBlock.Name, blkMyBlock.IsXRef, blkMyBlock.OwnerID
If blkMyBlock.IsXRef Then
If I < 0 Then I = 0
ReDim Preserve vList(0 To I)
vList(I) = blkMyBlock.Name
I = I + 1
End If
Next blkMyBlock
On Error Resume Next
If I > 0 Then
For I = 0 To UBound(vList)
ThisDrawing.Blocks.Item(vList(I)).Detach
Next I
End If
End Sub

Ciao.

Sean
0 Likes
Message 10 of 21

Anonymous
Not applicable
Hi Wayne,  At minimum any drawing will always
have *Model_Space and *Paper_Space.  Each layout added to the drawing will
add another *Paper_Space(0...n) entry to the block table.

 

I don't presume to be "knowledgeable",
but I've never let that stop me from commenting 🙂 
--
Bobby
C. Jones
www.AcadX.com


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I
profess to know next-to-nothing about AutoCAD, however I just did a bit of
investigation and on a new empty drawing I appear to have three Blocks
predefined:
   *Model_Space
   *Paper_Space

   *Paper_Space0
I suspect you can't get rid of these. If
that is true, a couple of solutions spring to mind (other than the Resume
Next). You could do an If-test inside the loop to make sure the Block.Name is
not one of these special names. Or you could skip these special ones by
changing the loop to:
 
For J = 3 to ThisDrawing.Blocks.Count - 1
  Set blockObjekt = ThisDrawing.Blocks(J)
  etc
However I don't know enough to know whether the number of predefined
blocks is constant, or the names of them are constant. Any knowledgable ones
care to comment?
  
Regards
  
Wayne Ivory

0 Likes
Message 11 of 21

Anonymous
Not applicable
"wivory" wrote in message
news:[email protected]...

> However I don't know enough to know whether the number of
> predefined blocks is constant, or the names of them are constant.

There's a minimum of two layouts (one model and one paper) but the
number of paper space layouts is open ended. To avoid processing layout
blocks, use the IsLayout property:

For Each blockDef In ThisDrawing.Blocks
If Not blockDef.IsLayout Then
' do your thing
End If
Next

--
Someone left the grass out in the yard all night.
http://www.acadx.com
0 Likes
Message 12 of 21

Anonymous
Not applicable
The trick lies in the fact, that you assume
that
blockObjekt
size=2>remains as is during the loop. It does
not.

 

The loop should be constructed with a
do...while statement checking the <COUNT> property  of the collection
you are stepping through.

 

face=Arial size=2>

--
Greetings, m.vr.gr.
W.K.M. van Rij

Brix Engineering

Gorinchem, Netherlands


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hi,

I have attached several XREFs in a drawing. After plotting
I need to
detach the XREFs. So far I use the following code:

Dim blockObjekt As AcadBlock

For Each blockObjekt In ThisDrawing.Blocks

if blockObjekt.IsXRef Then
      If
blockObjekt.name <> "" Then

           Debug.Print
blockObjekt.name

           ThisDrawing.Blocks.Item(blockObjekt.name).Detach

      End If
    End
If

Next blockObjekt

The first XREF is detached, after that I get a Runtime Error -2147467259
and the routine ends.

Does anybody know what happens?

Carmen

0 Likes
Message 13 of 21

Anonymous
Not applicable
Aaaha,

Thanks for that observation, I need to think outside my box.

Thanks.

Sean Bartleet

"w.k.m. van Rij" wrote in message
news:[email protected]...
> The trick lies in the fact, that you assume that blockObjekt remains as is
during the loop. It does not.
>
> The loop should be constructed with a do...while statement checking the
property of the collection you are stepping through.
>
>
> --
> Greetings, m.vr.gr.
> W.K.M. van Rij
> Brix Engineering
> Gorinchem, Netherlands
0 Likes
Message 14 of 21

Anonymous
Not applicable
Hi,

Why does the following code give me a runtime error -2145386475 Duplicate
key?
This seems to occur when I have at least two xref's attached where one of
them includes a nested xref. The nested xref does not necessarily have the
same name as the other xref.

Sub TTest()
Dim I As Integer
I = 0
While I < ThisDrawing.Blocks.Count
Debug.Print ThisDrawing.Blocks.Item(I).Name,
ThisDrawing.Blocks.Item(I).IsXRef
Set blkMyBlock = ThisDrawing.Blocks.Item(I)
If ThisDrawing.Blocks.Item(I).IsXRef Then
ThisDrawing.Blocks.Item(I).Detach
End If
I = I + 1
Wend

Does anyone know how to test for nested xrefs?

Thanks in anticipation.

Sean Bartleet

"w.k.m. van Rij" wrote in message
news:[email protected]...
> The trick lies in the fact, that you assume that blockObjekt remains as is
during the loop. It does not.
>
> The loop should be constructed with a do...while statement checking the
property of the collection you are stepping through.
>
>
> --
> Greetings, m.vr.gr.
> W.K.M. van Rij
> Brix Engineering
0 Likes
Message 15 of 21

Anonymous
Not applicable
The solution is to restart your FOR EACH loop after
each detachment:

 

restart:
    For Each objblk In
xdwg.Blocks
        If objblk.IsXRef =
True Then
           
objblk.Detach
           
GoTo restart
        End
If
    Next objblk


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hi,

I have attached several XREFs in a drawing. After plotting
I need to
detach the XREFs. So far I use the following code:

Dim blockObjekt As AcadBlock

For Each blockObjekt In ThisDrawing.Blocks

if blockObjekt.IsXRef Then
      If
blockObjekt.name <> "" Then

           Debug.Print
blockObjekt.name

           ThisDrawing.Blocks.Item(blockObjekt.name).Detach

      End If
    End
If

Next blockObjekt

The first XREF is detached, after that I get a Runtime Error -2147467259
and the routine ends.

Does anybody know what happens?

Carmen

0 Likes
Message 16 of 21

Anonymous
Not applicable
Hi,

That works but still gives an error -2145386475 Duplicate key when you try
and detach a drawing that occurs both as a primary xref and as a nested
xref, unless you happen to detach the drawing with the nested xref first.

I am still looking for any information on how to detect a duplicate xref.
(Without searching through all the block names and checking for duplicates)

But thanks for the code tip.

Regards

Sean Bartleet

"RogueDrafter" wrote in message
news:[email protected]...
> The solution is to restart your FOR EACH loop after each detachment:
>
> restart:
> For Each objblk In xdwg.Blocks
> If objblk.IsXRef = True Then
> objblk.Detach
> GoTo restart
> End If
> Next objblk
> "xiexieanke" wrote in message
news:[email protected]...
> Hi,
> I have attached several XREFs in a drawing. After plotting
> I need to detach the XREFs. So far I use the following code:
> Dim blockObjekt As AcadBlock
>
> For Each blockObjekt In ThisDrawing.Blocks
>
> if blockObjekt.IsXRef Then
> If blockObjekt.name <> "" Then
> Debug.Print blockObjekt.name
> ThisDrawing.Blocks.Item(blockObjekt.name).Detach
> End If
> End If
>
> Next blockObjekt
>
> The first XREF is detached, after that I get a Runtime Error -2147467259
and the routine ends.
>
> Does anybody know what happens?
>
> Carmen
>
0 Likes
Message 17 of 21

Anonymous
Not applicable
I'm wondering why you would bring in an XREF twice I guess. Have you
considered (or would it work for you) using the Overlay option when you
bring in an XREF?

"Sean Bartleet" wrote in message
news:[email protected]...
> Hi,
>
> That works but still gives an error -2145386475 Duplicate key when you try
> and detach a drawing that occurs both as a primary xref and as a nested
> xref, unless you happen to detach the drawing with the nested xref first.
>
> I am still looking for any information on how to detect a duplicate xref.
> (Without searching through all the block names and checking for
duplicates)
>
> But thanks for the code tip.
>
> Regards
>
> Sean Bartleet
>
> "RogueDrafter" wrote in message
> news:[email protected]...
> > The solution is to restart your FOR EACH loop after each detachment:
> >
> > restart:
> > For Each objblk In xdwg.Blocks
> > If objblk.IsXRef = True Then
> > objblk.Detach
> > GoTo restart
> > End If
> > Next objblk
> > "xiexieanke" wrote in message
> news:[email protected]...
> > Hi,
> > I have attached several XREFs in a drawing. After plotting
> > I need to detach the XREFs. So far I use the following code:
> > Dim blockObjekt As AcadBlock
> >
> > For Each blockObjekt In ThisDrawing.Blocks
> >
> > if blockObjekt.IsXRef Then
> > If blockObjekt.name <> "" Then
> > Debug.Print blockObjekt.name
> > ThisDrawing.Blocks.Item(blockObjekt.name).Detach
> > End If
> > End If
> >
> > Next blockObjekt
> >
> > The first XREF is detached, after that I get a Runtime
Error -2147467259
> and the routine ends.
> >
> > Does anybody know what happens?
> >
> > Carmen
> >
>
>
0 Likes
Message 18 of 21

Anonymous
Not applicable
Hi,

I just want to make my code robust so that if works for all situations.

Indeed the overlay is often the way to go. I have been particularly slack in
using this feature. I frequently discover situations where this has
occurred. I guess that I should be trapping this situation and reminding the
user through some obnoxious message to use overlays in future.

Thanks for the tip.

Sean

"RogueDrafter" wrote in message
news:[email protected]...
> I'm wondering why you would bring in an XREF twice I guess. Have you
> considered (or would it work for you) using the Overlay option when you
> bring in an XREF?
0 Likes
Message 19 of 21

Anonymous
Not applicable

You might want to look at XLOADCTL system variable. I have had success
setting this variable on all my users machines. Sometimes without them knowing
it (through code they use or by walking up to there computer and setting it).
Once it is set (to 2) overlaying will be in place. Most users have never seen
this variable and therefore are unlikely to change it.


Regards,


Rogue


 



style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hi,

I have attached several XREFs in a drawing. After plotting
I need to
detach the XREFs. So far I use the following code:

Dim blockObjekt As AcadBlock

For Each blockObjekt In ThisDrawing.Blocks

if blockObjekt.IsXRef Then
      If
blockObjekt.name <> "" Then

           Debug.Print
blockObjekt.name

           ThisDrawing.Blocks.Item(blockObjekt.name).Detach

      End If
    End
If

Next blockObjekt

The first XREF is detached, after that I get a Runtime Error -2147467259
and the routine ends.

Does anybody know what happens?

Carmen

0 Likes
Message 20 of 21

Anonymous
Not applicable

You might want to look at XLOADCTL system variable. I have had success
setting this variable on all my users machines. Sometimes without them knowing
it (through code they use or by walking up to there computer and setting it).
Once it is set (to 2) overlaying will be in place. Most users have never seen
this variable and therefore are unlikely to change it.


Regards,


Rogue


 



style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hi,

I have attached several XREFs in a drawing. After plotting
I need to
detach the XREFs. So far I use the following code:

Dim blockObjekt As AcadBlock

For Each blockObjekt In ThisDrawing.Blocks

if blockObjekt.IsXRef Then
      If
blockObjekt.name <> "" Then

           Debug.Print
blockObjekt.name

           ThisDrawing.Blocks.Item(blockObjekt.name).Detach

      End If
    End
If

Next blockObjekt

The first XREF is detached, after that I get a Runtime Error -2147467259
and the routine ends.

Does anybody know what happens?

Carmen

0 Likes