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

VBA Add document then Add layer got -2147418111 Automation error

3 REPLIES 3
Reply
Message 1 of 4
jeephong.chan
710 Views, 3 Replies

VBA Add document then Add layer got -2147418111 Automation error

Dear all,

 

   I got an error when add a layer after creating a new document, do you have any suggession?

 

Env.

 Sub test()
    On Error GoTo err_handle
    
    Dim app As AcadApplication
    Dim sleepCount As Long

    Set app = GetObject(, "AutoCAD.Application")
    For i = 1 To 10
        sleepCount = 0
        app.Documents.add "acadiso.dwt"
        
        'AddDrawingInfoLayer 'if I use this line directly,then will get an error: -2147418111 Automation error
        ' wait until the layer created, it's very slow, mornally it taks a long time to do it..
        Do While AddDrawingInfoLayer = False
            sleepCount = sleepCount + 1
            Wait 0.01' wait 0.01 second
        Loop
        
        Debug.Print i & " sleep " & sleepCount & " seconds"
        ThisDrawing.SaveAs "C:\temp\" & i & ".dwg"
        ThisDrawing.Close False
    Next i
    Exit Sub
    
err_handle:
    Debug.Print Err.Number
    Debug.Print Err.Description
End Sub

Private Function AddDrawingInfoLayer() As Boolean
    On Error GoTo err_handle
    ThisDrawing.Layers.add "new_layer"
    AddDrawingInfoLayer = True
    Exit Function
err_handle:
    AddDrawingInfoLayer = False
End Function

Private Function Wait(seconds As double)
    Dim varStart As Variant
    varStart = Timer
    Do While Timer < varStart + seconds
    Loop
End Function

 

   Autocad 2013 64bit, windows 8 64bit. 8GB ram and i7-3770 cpu

 

Thanks in advance!

 

 

 

3 REPLIES 3
Message 2 of 4

Hi,

 

>> I got an error when add a layer after creating a new document

Sorry, I don't get exceptions when I run your code without time-loops (which should not be necessary at least).

The only idea I have is you have anything running parallel that "reacts" on layer additions/modifications, e.g. do you have the layermanager open while running your code? Or do you have any applications/apps/tools installed?

 

What you might try "DoEvents" instead of the time-loops:

AddDrawingInfoLayer 'if I use this line directly,then will get an error: -2147418111 Automation error
DoEvents
' wait until the layer created, it's very slow, mornally it taks a long time to do it..
'        Do While AddDrawingInfoLayer = False
'            sleepCount = sleepCount + 1
'            Wait 0.01 ' wait 0.01 second
'        Loop

And - I guess you know that - AutoCAD 2013 is not supported for Win8.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 4

Thanks very much for your reply!

 

  I don't know why, if I delete wait code, then the layer cannot created, and if I change to "Wati 0", then i can work. Strange. I will test your code, thanks again!

Message 4 of 4

Hi,

 

>> I don't know why

Again the question: do you have the layermanager open while your code runs (and crashes)?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)

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

Post to forums  

Autodesk Design & Make Report

”Boost