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

Infamous error #462 ocurring alternatively

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Pablo_1985
521 Views, 5 Replies

Infamous error #462 ocurring alternatively

Hello everybody!

 

I know there is a lot been said about this error, tipically the code works aternatevely, so works fine, next time it doesn´t, then it does, then it doesn´t...

Even myself I´ve fix this errors in my code several times, but I cannot see what´s wrong this time :S, here is the line where it gets stopped:

 

Path = AutoCAD.AcadApplication.Path & "\Template\" & "Helloworld.lsp"

I know normally it happens due not specifying the whole property , but here I have tried everything already. What I do is putting a LSP file in typically "C:/Program files/Autodesk/AutoCAD 2014/Template/" where Autocad does not prompt any warning for being an unsafe location for loading a autolisp., but I´m not sure if in some other computer this path could change, so I build the complete path from the "Autocad.exe" file

 

Last time I had this error using two subs for opening and closing Autocad, specificall in the save as part. The problem stopped once I I did the following:

'Next line gives 462 error
AutoCAD.Application.ActiveDocument.SaveAs (Nombre)
'Next line avoids 462 error
acadApp.ActiveDocument.SaveAs (Nombre)

 

 And the above code inside the whole sub:

'**********************************************AcadOpen**********************************************
Sub AcadOpen()
    Dim acadApp As Object
    Dim acadDoc As Object
        
 'Comprueba si Autocad está abierto, si no está abierto crea una nueva instancia y la hace visible.
    On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
    End If
    
    ' Comprobar otra vez si hay un objeto AutoCAD
    If acadApp Is Nothing Then
        MsgBox "Lo siento, no se puede abrir Autocad!", vbCritical, "AutoCAD Error"
        Exit Sub
    End If
    On Error GoTo 0
    
    'Si no hay un dibujo activo crear uno
    On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
    If acadDoc Is Nothing Then
        Set acadDoc = acadApp.Documents.Add
    End If
    On Error GoTo 0

'tanto cerrar documento como autocadnecesitan que acadApp se inicialize con "set" y getobject como se hace arriba
'Cerrar documento
'acadApp.Documents.Close
'Cerrar autocad
'acadApp.Quit

'liberar objetos: no consigo quitar el error 462 con las siguientes 3 lineas
Set acadApp = Nothing
Set acadDoc = Nothing
On Error GoTo 0
End Sub

'********************'*********************Cerrar documento abierto********************'*********************
Sub cerrar()
   Dim acadApp As Object
   Dim Nombre As String
  
'obteniendo dirección completa del archivo que está en la celda A2
  Sheets("Pruebas").Select
  Nombre = Range("A2").Text

'inicializar la variable acadapp en el archivo que haya abierto.
Set acadApp = GetObject(, "AutoCAD.Application")

'guardar como
'Next line gives 462 error
AutoCAD.Application.ActiveDocument.SaveAs (Nombre)
'Next line avoids 462 error
acadApp.ActiveDocument.SaveAs (Nombre)

'Cerrar documento, no usándose ahora
'acadApp.Documents.Close
'Cerrar autocad
acadApp.Quit
End Sub

And to be honest I don´t see the logic in how I fixed it. I´ve been trying for hours to fix this error and for me it seems there is something fundamental about VBA that I´m missing... any idea?

 

Thank you very much in advance and forget my clumsiness believe it or not I´m stucked on something like this.

 

Pablo

 

 

 

 

5 REPLIES 5
Message 2 of 6
Pablo_1985
in reply to: Pablo_1985

Anybody thinks I should give more information?

Message 3 of 6
norman.yuan
in reply to: Pablo_1985

You actually have known what the problem is:

 

<QUOTE>

'Next line gives 462 error
AutoCAD.Application.ActiveDocument.SaveAs (Nombre)


'Next line avoids 462 error
acadApp.ActiveDocument.SaveAs (Nombre)

</QUOTE>

 

I am not sure why you need that line of code there. You just need to remove:

 

AutoCAD.Application.ActiveDocument.SaveAs (Nombre)

Message 4 of 6
Pablo_1985
in reply to: norman.yuan

Hi norman.yuan,

 

Thank you for your reply.

 

I might have not explain myself clearly enough, let me try again: that piece of code is from a code where I had the very same problem and I solved it doing as you said. Now the error is here:

 

Path = AutoCAD.AcadApplication.Path & "\Template\" & "Helloworld.lsp"

Whenever I try to use AtutoCAD.AcadApplication.path I get the 462 error.

 

I put the other piece of code as an example where I fixed already the code.

 

Thank you very much in advance,

 

Pablo

 

Message 5 of 6
norman.yuan
in reply to: Pablo_1985

When you automate AutoCAD from your Excel side in Excel VBA code, you need to obtain an running AutoCAD instance.

 

Excel VBA will automatically create an AutoCAD instance if you use code like 

 

AutoCAD.AcadApplication.xxxxx

 

if there is no AutoCAD session is running. Oddly enough, however, if there is an AutoCAD session running, referring AutoCAD.AcadApplication.xxxx will get you error 462.

 

The good practice is to use yor own code to obtain AutoCAD instance, and work with it, which you have already done iin your Sub AcadOpen(). You should declare the reference acadApp varaiable in module level, so that you can use it in other procedure/function in the same module for any AutoCAD related operation, and DO NOT USE AutoCAD.AcadApplication.xxx ANYWHERE.

 

For example, if you need to know AutoCAD installation path, you just call

 

Path = acadApp.Path & "\xxxxx"

 

 

 

 

Message 6 of 6
Pablo_1985
in reply to: norman.yuan

That solved the error completely! and not only that that was part of a sub that was before another sub who opened autocad, so everything was a mess... Now i run the sub wich opens autocad, then get the already opened instance, so basically what solved the problem was adding that part of the code you told me:

 

Dim AcadApp As Object

'whatever things more you want to so in your code:

On Error Resume Next
Set AcadApp = GetObject(, "AutoCAD.Application")
MsgBox "Sorry, there is not an Autocad instance" & Path & "!", vbCritical, "AutoCAD Error"

'whatever things more you want to so in your code:
Path = AcadApp.Path & "\Template\" & "HolaMundo.lsp"

Set AcadApp = Nothing

 

Now it runs error free and even better now I understand why it´s happening! thank you very much! 🙂

 

Pablo

 

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

Post to forums  

Autodesk Design & Make Report

”Boost