TheSwamp

Code Red => VB(A) => Topic started by: Yosso on December 09, 2015, 07:53:29 AM

Title: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: Yosso on December 09, 2015, 07:53:29 AM
Scenario:

I'm in Excel 2010 (32 bit, Win  7 64 bit).

Trying to create a block using an existing entity (a table)

AutoCAD 2015, running.

Code came from Fixo - http://forums.augi.com/showthread.php?137159-how-to-create-a-own-block-from-vba-programming

Code: [Select]
Dim blkDef As AcadBlock, _
    blkRef As AcadBlockReference, _
    oSset As AcadSelectionSet, _
    tempBlkName As String, _
    objColl(1) As Object, _
    tempObj As Object

tempBlkName = "_archive-Table" & Format(Now(), "hms")

origTblHndl = acadtbl.Handle
Set objColl(0) = adoc.HandleToObject(origTblHndl)
Set blkDef = adoc.Blocks.Add(tablept, tempBlkName)

AcadApp.ActiveDocument.CopyObjects objColl, blkDef

Code gives the Automation Error Catastraphic Error at the last line in the code excerpt above.

Anyone have any ideas (switch to dot.net?)  :-D

Thanks.

Mike
Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: 57gmc on December 09, 2015, 10:37:30 AM
I don't see where acadtbl gets set.
Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: Yosso on December 09, 2015, 02:02:15 PM
I don't see where acadtbl gets set.

The acadtbl comes in as an argument for the subroutine.

Code: [Select]
Sub PushRelayTable(AcadApp As AcadApplication, acadDoc As AcadDocument, acadtbl As AcadTable, datasheet As Worksheet, blnRelayCategory As Boolean)

I can see the table being added to the object collection while debugging.


Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: 57gmc on December 09, 2015, 05:50:34 PM
What's the status of blkdef? I don't see where that gets set either.
Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: Yosso on December 10, 2015, 06:23:01 AM
What's the status of blkdef? I don't see where that gets set either.

The very first line of the code block is where blkDef is dim'ed and the second line from the bottom is where the blkDef is set.

tablept is also set earlier in the module.  No error with any of the lines except the very last line.

Update (12/10/15)

Relocated the block creating routine back to the previous routine, now there is no error, but the newly created block does not contain the table.

The mystery deepens...



Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: 57gmc on December 10, 2015, 10:34:02 AM
The CopyObjects method only has two args, so the problem has to be one of them. You might not get an error, but blkDef could still be null. That's why I asked what is its status. Set a breakpoint on the CopyObjects line and see if blkdef is set or is null. Also, you might not get an error if you have used On Error Resume Next somewhere in your sub.
Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: Yosso on December 10, 2015, 11:19:05 AM
The CopyObjects method only has two args, so the problem has to be one of them. You might not get an error, but blkDef could still be null. That's why I asked what is its status. Set a breakpoint on the CopyObjects line and see if blkdef is set or is null. Also, you might not get an error if you have used On Error Resume Next somewhere in your sub.
You were so right, I didn't reset the error handler in that module.  Sigh.

Added the error handler back into that module (at the appropriate location) and ended up with the same error.

Based on the variable watch, both of the arguments have values. 

Pulling my hair on this one. 

I  even purchased an Autocad VBA book by Lee Ambrosius (AutoCAD Platform Customization) that was more up to date than my other ACAD VBA book. 

Based on his book my code should work.
Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: Cathy on December 10, 2015, 05:23:37 PM
Quote
Note You cannot execute this method while simultaneously iterating through a collection. An iteration will open the work space for a read-only operation, while this method attempts to perform a read-write operation. Complete any iteration before you call this method.


Just a WAG. 
Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: Yosso on December 17, 2015, 07:48:08 AM
Quote
Note You cannot execute this method while simultaneously iterating through a collection. An iteration will open the work space for a read-only operation, while this method attempts to perform a read-write operation. Complete any iteration before you call this method.


Just a WAG.

Appreciate the feedback, but I do not think I'm not iterating through any collections, am I? 

M.

Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: 57gmc on December 17, 2015, 10:17:52 AM
Quote
Note You cannot execute this method while simultaneously iterating through a collection. An iteration will open the work space for a read-only operation, while this method attempts to perform a read-write operation. Complete any iteration before you call this method.


Just a WAG.

Appreciate the feedback, but I do not think I'm not iterating through any collections, am I? 

M.


Not at the time you are calling the method.

Perhaps if you uploaded your spreadsheet and a sample dwg with the steps to reproduce the problem, someone could troubleshoot it. There's not much info here.
Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: Yosso on December 21, 2015, 08:07:24 AM
Quote
Note You cannot execute this method while simultaneously iterating through a collection. An iteration will open the work space for a read-only operation, while this method attempts to perform a read-write operation. Complete any iteration before you call this method.


Just a WAG.

Appreciate the feedback, but I do not think I'm not iterating through any collections, am I? 

M.


Not at the time you are calling the method.

Perhaps if you uploaded your spreadsheet and a sample dwg with the steps to reproduce the problem, someone could troubleshoot it. There's not much info here.

Code: [Select]
Dim blkDef As AcadBlock, _
    blkRef As AcadBlockReference, _
    tempBlkName As String, _
    origTblHndl As String, _
    objColl(0) As Object, _
    retObj As Object
   
tempBlkName = "archive-Table" & Format(Now(), "hms")

' Set tempEntity = ListTable
origTblHndl = ListTable.Handle
Set objColl(0) = adoc.HandleToObject(origTblHndl)
Set blkDef = adoc.Blocks.Add(tablept, tempBlkName)

' Copy old table to block
adoc.CopyObjects objColl, blkDef

The open collection was objColl.  Once objColl was dim'ed to objColl(0) everything worked.

Thank you Cathy for the assistance.




Title: Re: Automation Error - Catastrophic Error - VBA Excel create new block in ACAD 2015
Post by: Yosso on December 22, 2015, 09:41:37 AM
Problem solved, thanks to Cathy's WAG.

Set the array size to 0 and all was good.

Thanks again.