Visual Basic Customization

Reply
Member
jeephong.chan
Posts: 3
Registered: ‎04-06-2011
Message 1 of 4 (244 Views)

VBA Add document then Add layer got -2147418111 Automation error

244 Views, 3 Replies
05-08-2013 06:34 PM

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!

 

 

 

*Expert Elite*
Alfred.NESWADBA
Posts: 8,469
Registered: ‎06-29-2007
Message 2 of 4 (229 Views)

Re: VBA Add document then Add layer got -2147418111 Automation error

05-08-2013 09:40 PM in reply to: jeephong.chan

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
-------------------------------------------------------------------------
Member
jeephong.chan
Posts: 3
Registered: ‎04-06-2011
Message 3 of 4 (219 Views)

Re: VBA Add document then Add layer got -2147418111 Automation error

05-09-2013 03:54 AM in reply to: Alfred.NESWADBA

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!

*Expert Elite*
Alfred.NESWADBA
Posts: 8,469
Registered: ‎06-29-2007
Message 4 of 4 (213 Views)

Re: VBA Add document then Add layer got -2147418111 Automation error

05-09-2013 10:31 AM in reply to: jeephong.chan

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
-------------------------------------------------------------------------

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community