Insert block from container drawing

Insert block from container drawing

h_s_walker
Mentor Mentor
487 Views
8 Replies
Message 1 of 9

Insert block from container drawing

h_s_walker
Mentor
Mentor

Ok so chatgpt gave me the following code to insert a specific block from a container drawing (it has mutliple blocks in it) into AutoCAD. The problem I have with it is everytime I run it I get the following error (see the image below). Are there any suggestions for how to fix it?

h_s_walker_0-1752237123315.png

 

Sub InsertBlockFromContainer()
    Dim acadApp As AcadApplication
    Dim acadDoc As AcadDocument
    Dim insertPoint(0 To 2) As Double
    Dim blockName As String
    Dim blockPath As String
    Dim insertedBlock As AcadBlockReference

    ' Set the block name (must match the block name in the container DWG)
    blockName = "RIBPOT"

    ' Full path to the container drawing
    blockPath = "C:\Users\Howard\AppData\Roaming\Autodesk\AutoCAD 2026\R25.1\enu\Support\Probord.dwg"

    ' Insertion point in current drawing
    insertPoint(0) = 0: insertPoint(1) = 0: insertPoint(2) = 0

    ' Get AutoCAD application and active document
    Set acadApp = ThisDrawing.Application
    Set acadDoc = acadApp.ActiveDocument

    ' Insert block from external DWG
    On Error Resume Next
    acadDoc.SendCommand "-INSERT " & Chr(34) & blockPath & "=" & blockName & Chr(34) & " " & _
                        insertPoint(0) & "," & insertPoint(1) & "," & insertPoint(2) & " 1 1 0 " & vbCr

    ' Wait for command to complete
    acadApp.Update
    MsgBox "Block inserted successfully.", vbInformation
End Sub

  

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Accepted solutions (1)
488 Views
8 Replies
Replies (8)
Message 2 of 9

norman.yuan
Mentor
Mentor

Code from Chatgpt can only be used with caution by EXPERIENCED programmer, I mean who is experience ENOUGH that Chatgpt is only used when he/she feels lazy to have  write all lines of code and knowing the code from Chatgpt could be entirely useless. Oh, you may have to ask Chatgpt VERY ACCURATELY about what you need from it.

 

In your case, you have want insert a block from a drawing file, which is not a block drawing, rather it contains the block and other contents, block or not. Also, the drawing probably not only contains the block definition, but also one or more block references. It is possible, eh?

 

So, what you want to do, as programmer, is to (knowing a block name, and want a block reference to be inserted at a given location):

 

1. first test if the current drawing has the block definition existing. You can go through ThisDrawing.Blocks collection; if yes, skip 2, and go to 3;

2. Make sure the source/containing drawing is open already. Then:

a. Go though that drawing's AcadBlocks collection to find the block definition with the name (AcadBlock);

b. If not found, the entire process ends, of course, When found, you COPY the block definition (AcadBlock object) into the destination drawing, using AcadDocument.CopyObject() method. After this, the target block's definition is now available in the drawing;

3. Now you can call AcadModel[Paper]Space.InsertBlock() method do insert a reference of the block at given location (DO NOT use SendCommand() to do the inserting, unless in very, very rare cases!).

 

Not knowing your VBA programming knowledge level, my description here is meant for you to know how wrong/how useless the Chatgpt code could be in may cases. But the real code that works as I described is not too difficult to write, if you do have Acad VBA coding experience. If you indeed need a working code, let me know.

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 9

Ed__Jobe
Mentor
Mentor

Hey Howard. The code you show is hard-coded to work only on a block named "RIBPOT". If the container dwg doesn't have this, it will fail. This workflow is already available if you use Design Center. However, if you want a command, using Norman's suggestions, I would:

1. prompt the user to select a container dwg, defaulting to the last selected file.

2. Load a form with checkboxes for the blocks stored in container dwg for the user to select the files to insert.

3. When the user selects OK, the blocks are inserted. You can prompt the user to select a point for each block to be inserted.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 9

h_s_walker
Mentor
Mentor

I know it's hardcoded for RIBPOT, that is one of the more complicated blocks I made, and if I can get the code for that working it's easy to change it to work for other blocks

 

1. The container drawing is always the same drawing. I make the blocks put them in the drawing and then distribute it to everyone.

2. I'm trying to do that, but first I want to get the code working to insert the block, and then I can work on a list.

3. I'm not too fussed about positioning yet.

 

The program eventually will ask for the block, then load a load of parameters from excel, and use those parameters to make the block the correct size

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 5 of 9

norman.yuan
Mentor
Mentor
Accepted solution

Here is the working code that does what you want by knowing: 1. the block-containing dwg file; 2. the target block name; and 3. the insertion point in current drawing (I use ObjectDBX to read the source drawing into memory, instead of visually/slowly opening it in AutoCAD editor):

Option Explicit

Public Sub InsertBlockFromOtherDrawing()

    Dim blkContainerDwg As String
    Dim blkName As String
    Dim pt(0 To 2) As Double
    
    blkContainerDwg = "C:\Temp\MyBlockLib.dwg"
    blkName = "TestBlock01"
    pt(0) = 100#: pt(1) = 100#: pt(2) = 0#
    
    
    InsertBlock blkContainerDwg, blkName, pt
    
End Sub

Private Sub InsertBlock(sourceFile As String, blkName As String, insPoint As Variant)
    
    If Not InsertBlockDefinition(sourceFile, blkName) Then
        Exit Sub
    End If
    
    ThisDrawing.ModelSpace.InsertBlock insPoint, blkName, 1#, 1#, 1#, 0#
    
End Sub

Private Function InsertBlockDefinition(sourceDwg As String, blkName As String) As Boolean

    If BlockDefExists(blkName) Then
        InsertBlockDefinition = True
        Exit Function
    End If

    On Error Resume Next

    Dim sourceDoc As AXDBLib.AxDbDocument
    Set sourceDoc = ThisDrawing.Application.GetInterfaceObject("ObjectDBX.AxDbDocument.24")
    sourceDoc.Open sourceDwg
    If Err.Number <> 0 Then
        MsgBox "Open block source file failed!"
        InsertBlockDefinition = False
        Exit Function
    End If
    
    Dim blk As AcadBlock
    Dim blks(0) As AcadObject
    Dim copied As Boolean
    For Each blk In sourceDoc.Blocks
        If UCase(blk.Name) = UCase(blkName) Then
            Set blks(0) = blk
            sourceDoc.CopyObjects blks, ThisDrawing.Blocks
            If Err.Number <> 0 Then
                MsgBox "Copying block definition failed: " & vbCrLf & Err.Description
                copied = False
            Else
                copied = True
            End If
            Exit For
        End If
    Next

    Set sourceDoc = Nothing
    InsertBlockDefinition = copied
    
End Function

Private Function BlockDefExists(blkName As String) As Boolean

    Dim blk As AcadBlock
    For Each blk In ThisDrawing.Blocks
        If UCase(blk.Name) = UCase(blkName) Then
            BlockDefExists = True
            Exit Function
        End If
    Next
    BlockDefExists = False
End Function

 

Also, since I use Acad2024, thus the ProgId for ObjectDBX's document is "ObjectDBX.AxdbDocument.24" (for 2022 to 2024). If you use Acad2025/6, it should be "xxxx.xxxxx.25".

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 9

h_s_walker
Mentor
Mentor

@norman.yuan Don't worry I found the error. I hadn't added the AutoCAD objectdbx 25 library

 

 

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 7 of 9

Ed__Jobe
Mentor
Mentor

Hi @h_s_walker , are you seeking to have a command for each block you want to insert, or just something that points to a standard library?

I already mentioned Design Center (ADCENTER), but BLOCKSPALETTE can automatically load blocks from a library, which can be a dwg or folder. See below.

2025-07-18_17-01-24.PNG

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 8 of 9

h_s_walker
Mentor
Mentor

@Ed__Jobe No we don't want it like that. I know about block palette.

What I'm trying to do is make it so that it can be used in LT (probably not possible). Where you choose a block, then it will get a list of values from excel and use those values to "resize" the block

Howard Walker
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Left Handed and Proud

0 Likes
Message 9 of 9

Ed__Jobe
Mentor
Mentor

LT can't run VBA. Only the base lisp, no vl or vlx or AcitveX. To communicate with xl requires ActiveX.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature